[英]How to add Jbutton from another class to main class
So am I trying to add JButton from another class into my main class, but I have no idea how to. 因此,我是否试图将另一个类中的JButton添加到我的主类中,但是我不知道该如何做。 Do I have to use a certain command or import a package to add the button? 我是否必须使用某个命令或导入程序包来添加按钮?
//my first class with JButton
public class ScreenInitial
{
public ScreenInitial()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
JButton newArrival = new JButton("New Arrival");
panel.add(newArrival);
}
}
//my main class
public class FurryFriendsAnimalShelter extends JFrame
{
public static void main(String[] args)
{
JFrame window = new JFrame("FFAS");
Toolkit tk = Toolkit.getDefaultToolkit();
int widthScreen = ((int)tk.getScreenSize().getWidth());
int lengthScreen = ((int) tk.getScreenSize().getWidth());
window.setSize(widthScreen,lengthScreen);
window.getContentPane().setBackground(Color.BLACK);
window.show(true);
}
}
The easiest way is to extend some JComponent
type and let everything rely on that class. 最简单的方法是扩展某些JComponent
类型,并使所有内容都依赖该类。
public class NewClass {
public static void main(String[] args) {
JFrame window = new JFrame("Furry Friends Animal Shelter");
Toolkit tk = Toolkit.getDefaultToolkit();
int widthScreen = ((int) tk.getScreenSize().getWidth());
int lengthScreen = ((int) tk.getScreenSize().getWidth());
window.setSize(widthScreen, lengthScreen);
window.getContentPane().setBackground(Color.BLACK);
window.add(new ScreenInitial());
window.show(true);
}
public static class ScreenInitial extends JPanel {
public ScreenInitial() {
setLayout(new GridLayout(0, 1));
JButton newArrival = new JButton("New Arrival");
add(newArrival);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.