unionSP
Novembro 10, 2007, 8:12pm
#1
Prezados,
No codigo abaixo como faço para deixar o criaMostraGUI() como true quando for gerado um evento em um JMenuItem.
[code]private void criaMostraGui() {
JFrame frame = new JFrame("Hello");
JLabel label = new JLabel("Hello, Swing World");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(false);
}
private void createAndShowGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMenuBar());
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(scrollPane);
setLocation(150, 200);
setSize(450, 260);
setVisible(true);
}
public static void main(String[] args) {
ExemploJMenu exemploJMenu = new ExemploJMenu("Quina Muita Sorte 2007");
exemploJMenu.createAndShowGUI();
exemploJMenu.criaMostraGui();
}
public void actionPerformed(ActionEvent e) {
// checar se o evento é de JMenuItem
if (e.getSource() instanceof JMenuItem) {
JMenuItem menuItem = (JMenuItem) e.getSource();
String texto = menuItem.getText();
textArea.append("=> Evento gerado para JMenuItem " + texto);
textArea.append("\n");
if ("Sair".equals(texto)) {
int opcao = JOptionPane.showConfirmDialog(this,
"Deseja realmente sair?", "Sair",
JOptionPane.YES_NO_OPTION);
if (opcao == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
}
}[/code]
_Renatu
Novembro 10, 2007, 10:34pm
#2
hm, eu nao entendi muito bem sua pergunta…
O que voce quer deixar como true?
voce quer exibir aquele GUI?
voce pode simplesmente fazer frame.setVisible(true);
o problema que talvez voce tenha ai é de escopo…
esta variavel “frame” precisa estar disponivel num escopo fora do metodo criaMostraGui, para que possa ser acessada de outro metodo, como o actionPerformed, por exemplo…
Cara eu tb nao entendi mas pelo q eu entendi eu acho q vc deve criar uma variavel tipo:
[code] public void actionPerformed(ActionEvent e) {
// checar se o evento é de JMenuItem
if (e.getSource() instanceof JMenuItem) {
JMenuItem menuItem = (JMenuItem) e.getSource();
String texto = menuItem.getText();
textArea.append("=> Evento gerado para JMenuItem " + texto);
textArea.append("\n");
if (“Sair”.equals(texto)) {
int opcao = JOptionPane.showConfirmDialog(this,
“Deseja realmente sair?”, “Sair”,
JOptionPane.YES_NO_OPTION);
if (opcao == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
}
}
private void criaMostraGui() {
JFrame frame = new JFrame(“Hello”);
JLabel label = new JLabel(“Hello, Swing World”);
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(false);
}
private void createAndShowGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMenuBar());
JScrollPane scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
getContentPane().add(scrollPane);
setLocation(150, 200);
setSize(450, 260);
setVisible(true);
}
public static void main(String[] args) {
ExemploJMenu exemploJMenu = new ExemploJMenu("Quina Muita Sorte 2007");
exemploJMenu.createAndShowGUI();
exemploJMenu.criaMostraGui();
}
public void actionPerformed(ActionEvent e) {
// checar se o evento é de JMenuItem
// cria uma variavel de classe aiai
aiai=true;
if (e.getSource() instanceof JMenuItem) {
JMenuItem menuItem = (JMenuItem) e.getSource();
String texto = menuItem.getText();
textArea.append("=> Evento gerado para JMenuItem " + texto);
textArea.append("\n");
if ("Sair".equals(texto)) {
int opcao = JOptionPane.showConfirmDialog(this,
"Deseja realmente sair?", "Sair",
JOptionPane.YES_NO_OPTION);
if (opcao == JOptionPane.OK_OPTION) {
System.exit(0);
}
}
}else aiai=false;
}
public boolean aiaiai()
{
return aiai;
}
[/code]
Eu devo ta viajando …tb eu to bebado hehehehehehehe