所以我试图将JButton从另一个类添加到我的主类中,但我不知道如何。我是否必须使用某个命令或导入包来添加按钮?
//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);
}
}
答案 0 :(得分:0)
最简单的方法是扩展一些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);
}
}
}