Boa Tarde!!
Alguém pode me ajudar??
Não consigo nem substituir o ícone da janela e nem incluir um ícone no(s) botão(ões)… Depois de substituir o ícone da janela (linhas 20, 26 e 28) e compilar, quando executo a janela fica sem ícone, bem como o botão “btn1” (linhas 118 e 120).
Segue abaixo o código.
Obrigado,
ffranco
[code]
package projectone;
import javax.swing.;
import java.awt.;
import java.awt.event.;
import java.util.;
import javax.swing.border.*;
import java.text.DecimalFormat;
public class MainFrame extends JFrame {
JLabel hours, date;
String dayweek[] = {"Domingo", "2ª Feira", "3ª Feira", "4ª Feira", "5ª Feira", "6ª Feira", "Sábado"};
String month[] = {"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
JTextField status;
JMenuItem files, sales, optns, accnt, invcn, exit, about;
Password wndw;
String user = "fabiano", password = "102010";
JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9;
ImageIcon icn01, icn02, icn03, icn04, icn05, icn06, icn07, icn08, icn09, icn10;
public MainFrame() {
super("Power Freight");
icn01 = new ImageIcon("ctn_sp.gif");
setIconImage(icn01.getImage());
setSize(1025, 740);
setLocation(0, 0);
setVisible(true);
setExtendedState(MAXIMIZED_BOTH);
setResizable(false);
Container mainwindow = getContentPane();
mainwindow.setLayout(null);
// -------------------------------------------------------------------------
// Preparação do MainFrame para Solicitação de Usuário e Senha
wndw = new Password(null, "Nome do Usuário e Senha", true);
wndw.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
wndw.setVisible(true);
// -------------------------------------------------------------------------
// Menu
// Barra de Menus
JMenuBar bar = new JMenuBar();
setJMenuBar(bar);
// Item do Menu
JMenu movements = new JMenu("Módulos");
movements.setMnemonic(KeyEvent.VK_A);
JMenu reports = new JMenu("Relatórios");
reports.setMnemonic(KeyEvent.VK_R);
JMenu tools = new JMenu("Ferramentas");
tools.setMnemonic(KeyEvent.VK_E);
JMenu help = new JMenu("Ajuda");
help.setMnemonic(KeyEvent.VK_J);
// SubItens do Menu
Treatment treat1 = new Treatment();
Treatment treat2 = new Treatment();
Treatment treat3 = new Treatment();
Treatment treat4 = new Treatment();
Treatment treat5 = new Treatment();
Treatment treat6 = new Treatment();
Treatment treat7 = new Treatment();
files = new JMenuItem("Cadastros");
sales = new JMenuItem("Comercial");
optns = new JMenuItem("Operações");
accnt = new JMenuItem("Financeiro");
invcn = new JMenuItem("Faturamento");
about = new JMenuItem("Sobre o Power Freight");
exit = new JMenuItem("Sair");
files.setMnemonic(KeyEvent.VK_C);
sales.setMnemonic(KeyEvent.VK_M);
optns.setMnemonic(KeyEvent.VK_O);
accnt.setMnemonic(KeyEvent.VK_F);
invcn.setMnemonic(KeyEvent.VK_T);
about.setMnemonic(KeyEvent.VK_P);
exit.setMnemonic(KeyEvent.VK_S);
files.addActionListener(treat1);
sales.addActionListener(treat2);
optns.addActionListener(treat3);
accnt.addActionListener(treat4);
invcn.addActionListener(treat5);
about.addActionListener(treat6);
exit.addActionListener(treat7);
// Adiciona SubItens aos Itens
movements.add(files);
movements.add(sales);
movements.add(optns);
movements.add(accnt);
movements.add(invcn);
movements.addSeparator();
movements.add(exit);
help.add(about);
// Adiciona Itens à Barra
bar.add(movements);
bar.add(reports);
bar.add(tools);
bar.add(help);
// -------------------------------------------------------------------------
// Componentes
icn02 = new ImageIcon("CP31.ico");
btn1 = new JButton("Cadastros", icn02);
btn2 = new JButton("Comercial");
btn3 = new JButton("Operações");
btn4 = new JButton("Financeiro");
btn5 = new JButton("Faturamento");
btn6 = new JButton("Relatórios");
btn7 = new JButton("Ferramentas");
btn8 = new JButton("Ajuda");
btn9 = new JButton("Sair");
btn1.setBounds(100, 100, 200, 90);
btn2.setBounds(100, 300, 200, 90);
btn3.setBounds(100, 500, 200, 90);
btn4.setBounds(400, 100, 200, 90);
btn5.setBounds(400, 300, 200, 90);
btn6.setBounds(400, 500, 200, 90);
btn7.setBounds(700, 100, 200, 90);
btn8.setBounds(700, 300, 200, 90);
btn9.setBounds(700, 500, 200, 90);
btn1.setMnemonic(KeyEvent.VK_C);
btn2.setMnemonic(KeyEvent.VK_M);
btn3.setMnemonic(KeyEvent.VK_O);
btn4.setMnemonic(KeyEvent.VK_F);
btn5.setMnemonic(KeyEvent.VK_T);
btn6.setMnemonic(KeyEvent.VK_R);
btn7.setMnemonic(KeyEvent.VK_E);
btn8.setMnemonic(KeyEvent.VK_J);
btn9.setMnemonic(KeyEvent.VK_S);
mainwindow.add(btn1);
mainwindow.add(btn2);
mainwindow.add(btn3);
mainwindow.add(btn4);
mainwindow.add(btn5);
mainwindow.add(btn6);
mainwindow.add(btn7);
mainwindow.add(btn8);
mainwindow.add(btn9);
Treatment treat8 = new Treatment();
btn9.addActionListener(treat8);
// -------------------------------------------------------------------------
// Painel Inferior
Border border = BorderFactory.createLoweredBevelBorder();
status = new JTextField(JTextField.CENTER);
status.setPreferredSize(new Dimension(720, 20));
status.setFont(new Font("Arial", Font.BOLD, 12));
status.setEditable(false);
status.setBorder(border);
hours = new JLabel("horas", JLabel.CENTER);
hours.setPreferredSize(new Dimension(75, 20));
hours.setBorder(border);
date = new JLabel("data", JLabel.CENTER);
date.setPreferredSize(new Dimension(215, 20));
date.setBorder(border);
JPanel bottonpanel = new JPanel();
bottonpanel.setLayout(new FlowLayout(FlowLayout.CENTER, 1, 0));
bottonpanel.add(status);
bottonpanel.add(hours);
bottonpanel.add(date);
BorderLayout lobp = new BorderLayout();
mainwindow.setLayout(lobp);
mainwindow.add(bottonpanel, BorderLayout.SOUTH);
// -------------------------------------------------------------------------
// Atualização da Data e do Relógio
ActionListener task = new ActionListener() {
public void actionPerformed(ActionEvent e) {
hoursDate();
}
};
javax.swing.Timer timer = new javax.swing.Timer(1000, task);
timer.start();
}
// -------------------------------------------------------------------------
// Método main()
public static void main(String args[]) {
final MainFrame app = new MainFrame();
app.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
app.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Object[] Btns = {" Sim ", " Não "};
int confirmexit = JOptionPane.showOptionDialog(null, "Deseja mesmo encerrar o Power Freight?", "Sair", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Btns, Btns[1]);
if(confirmexit == 0)
System.exit(0);
}
}
);
};
// -------------------------------------------------------------------------
// Funcionamento da Data e do Relógio
private void hoursDate() {
Calendar now = Calendar.getInstance();
int ho = now.get(Calendar.HOUR_OF_DAY);
int mi = now.get(Calendar.MINUTE);
int se = now.get(Calendar.SECOND);
int dw = now.get(Calendar.DAY_OF_WEEK);
int dy = now.get(Calendar.DAY_OF_MONTH);
int mt = now.get(Calendar.MONTH);
int yr = now.get(Calendar.YEAR);
hours.setText(format(ho % 24) + ":" + format(mi) + ":" + format(se) + "");
date.setText(dayweek[dw - 1] + ", " + format(dy) + " de " + month[mt] + " de " + yr + "");
}
private String format(int num) {
DecimalFormat df = new DecimalFormat("00");
return df.format(num);
}
// -------------------------------------------------------------------------
// Janela de Solicitação e Averiguação de Usuário e Senha
public void verify(String u, String p) {
if((u.equals(user)) && (p.equals(password)))
wndw.hide();
else {
JOptionPane.showMessageDialog(null, "Usuário e/ou Senha Incorretos");
wndw.txtUser.requestFocus();
}
}
private class Password extends JDialog {
JTextField txtUser;
JPasswordField txtPassword;
JButton enter, cancel;
public Password(Frame owner, String title, boolean modal) {
super(owner, title, modal);
Container mainwindow = getContentPane();
BorderLayout layout = new BorderLayout();
mainwindow.setLayout(layout);
JLabel lblUser = new JLabel("Nome do Usuário:");
JLabel lblPassword = new JLabel("Senha:");
txtUser = new JTextField(10);
txtPassword = new JPasswordField(10);
JPanel upper = new JPanel();
upper.setLayout(new GridLayout(2, 2, 5, 5));
upper.add(lblUser);
upper.add(txtUser);
upper.add(lblPassword);
upper.add(txtPassword);
JPanel upper2 = new JPanel();
String ttl = "Informe o Nome do Usuário e Senha";
Border etched = BorderFactory.createEtchedBorder();
Border border = BorderFactory.createTitledBorder(etched, ttl);
upper2.setBorder(border);
upper2.setLayout(new FlowLayout(FlowLayout.LEFT));
upper2.add(upper);
TreatUsPsw treat0 = new TreatUsPsw();
enter = new JButton("Entrar");
enter.addActionListener(treat0);
getRootPane().setDefaultButton(enter);
cancel = new JButton("Cancelar");
cancel.addActionListener(treat0);
JPanel lower = new JPanel();
lower.setLayout(new FlowLayout(FlowLayout.CENTER));
lower.add(enter);
lower.add(cancel);
mainwindow.add(BorderLayout.NORTH, upper2);
mainwindow.add(BorderLayout.SOUTH, lower);
setSize(280, 150);
setLocationRelativeTo(null);
}
private class TreatUsPsw implements ActionListener {
public void actionPerformed(ActionEvent e) {
String password = new String(txtPassword.getPassword());
// Teste Usuário e Senha
if (e.getSource() == enter) {
verify(txtUser.getText(), password);
}
else
System.exit(0);
}
}
}
// -------------------------------------------------------------------------
// Tratamento de Eventos do MainFrame
private class Treatment implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Indicacao na Barra de Status
if (e.getSource() instanceof JMenuItem) {
JMenuItem mi = (JMenuItem) e.getSource();
status.setText(mi.getText());
}
// Sobre o Power Freight
if(e.getSource() == about) {
String abt = "Power Freight Versão 1.0/2006";
JOptionPane.showMessageDialog(null, abt);
}
// Saída do Sistema
if(e.getSource() == exit) {
Object Butns[] = {" Sim ", "Não"};
int closing = JOptionPane.showOptionDialog(null, "Deseja mesmo encerrar o Power Freight?", "Sair", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Butns, Butns[1]);
if(closing == 0)
System.exit(0);
}
if(e.getSource() == btn9) {
Object Butns[] = {" Sim ", "Não"};
int closing = JOptionPane.showOptionDialog(null, "Deseja mesmo encerrar o Power Freight?", "Sair", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, Butns, Butns[1]);
if(closing == 0)
System.exit(0);
}
}
}
}[/code]