如何在不从另一个 class 创建新的 window 的情况下添加按钮
How to add a button without creating a new window from another class
也许我对这个概念的理解完全错误,但我正在尝试为按钮创建 classes 并在主 GUI class 中调用它们,以便它们出现在 window 上但我不完全知道如何将这些按钮 classes 调用到主 GUI class。
我目前通过简单地创建一个新对象来创建它们,但这会创建一个新的 window,而不是将其添加到我的主 GUI class 中当前存在的 window。我该如何解决这个问题,还是我这样做完全错了?
图形界面 class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GUI extends Frame implements WindowListener
{
public GUI()
{
super("GUI");
setLayout(new FlowLayout());
addWindowListener(this);
}
public void windowDeiconified(WindowEvent event){}
public void windowIconified(WindowEvent event){}
public void windowActivated(WindowEvent event){}
public void windowDeactivated(WindowEvent event){}
public void windowOpened(WindowEvent event){}
public void windowClosed(WindowEvent event) {}
public void windowClosing(WindowEvent e) { System.exit(0); }
public static void main(String[] args)
{
GUI screen = new GUI();
new Button1("TEST");
screen.setSize(500,100);
screen.setLocationRelativeTo(null);
screen.setVisible(true);
}
}
按钮 1 class
import java.awt.*;
import java.awt.event.*;
public class Button1 extends GUI implements ActionListener {
private Button addButton;
public Button1(String s) {
addButton = new Button(s);
add(addButton);
addButton.addActionListener(this);
setVisible(true);
System.out.println("It atleast works here");
}
public void actionPerformed(ActionEvent e) {
System.out.println("It works - Button1");
}
public static void main(String[] args) {
}
}
首先:为什么你真的想为每个按钮创建新的 类?
第二:是的,我认为你没有完全正确。这段代码应该做你想做的。如果您不明白,请阅读或观看 Java 中有关面向对象编程的教程。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GUI extends Frame implements WindowListener
{
public GUI()
{
super("GUI");
setLayout(new FlowLayout());
addWindowListener(this);
}
public void windowDeiconified(WindowEvent event){}
public void windowIconified(WindowEvent event){}
public void windowActivated(WindowEvent event){}
public void windowDeactivated(WindowEvent event){}
public void windowOpened(WindowEvent event){}
public void windowClosed(WindowEvent event) {}
public void windowClosing(WindowEvent e) { System.exit(0); }
public static void main(String[] args)
{
GUI screen = new GUI();
screen.setSize(500,100);
Button button1 = new Button("Button 1");
button1.addActionListener(new ActionListener() {
System.out.println("It works - Button1");
});
screen.add(button1);
screen.setLocationRelativeTo(null);
screen.setVisible(true);
}
}
也许我对这个概念的理解完全错误,但我正在尝试为按钮创建 classes 并在主 GUI class 中调用它们,以便它们出现在 window 上但我不完全知道如何将这些按钮 classes 调用到主 GUI class。
我目前通过简单地创建一个新对象来创建它们,但这会创建一个新的 window,而不是将其添加到我的主 GUI class 中当前存在的 window。我该如何解决这个问题,还是我这样做完全错了?
图形界面 class
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GUI extends Frame implements WindowListener
{
public GUI()
{
super("GUI");
setLayout(new FlowLayout());
addWindowListener(this);
}
public void windowDeiconified(WindowEvent event){}
public void windowIconified(WindowEvent event){}
public void windowActivated(WindowEvent event){}
public void windowDeactivated(WindowEvent event){}
public void windowOpened(WindowEvent event){}
public void windowClosed(WindowEvent event) {}
public void windowClosing(WindowEvent e) { System.exit(0); }
public static void main(String[] args)
{
GUI screen = new GUI();
new Button1("TEST");
screen.setSize(500,100);
screen.setLocationRelativeTo(null);
screen.setVisible(true);
}
}
按钮 1 class
import java.awt.*;
import java.awt.event.*;
public class Button1 extends GUI implements ActionListener {
private Button addButton;
public Button1(String s) {
addButton = new Button(s);
add(addButton);
addButton.addActionListener(this);
setVisible(true);
System.out.println("It atleast works here");
}
public void actionPerformed(ActionEvent e) {
System.out.println("It works - Button1");
}
public static void main(String[] args) {
}
}
首先:为什么你真的想为每个按钮创建新的 类? 第二:是的,我认为你没有完全正确。这段代码应该做你想做的。如果您不明白,请阅读或观看 Java 中有关面向对象编程的教程。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GUI extends Frame implements WindowListener
{
public GUI()
{
super("GUI");
setLayout(new FlowLayout());
addWindowListener(this);
}
public void windowDeiconified(WindowEvent event){}
public void windowIconified(WindowEvent event){}
public void windowActivated(WindowEvent event){}
public void windowDeactivated(WindowEvent event){}
public void windowOpened(WindowEvent event){}
public void windowClosed(WindowEvent event) {}
public void windowClosing(WindowEvent e) { System.exit(0); }
public static void main(String[] args)
{
GUI screen = new GUI();
screen.setSize(500,100);
Button button1 = new Button("Button 1");
button1.addActionListener(new ActionListener() {
System.out.println("It works - Button1");
});
screen.add(button1);
screen.setLocationRelativeTo(null);
screen.setVisible(true);
}
}