[英]Java Swing - Show multiple panels
我在 IntelliJ 中使用 Java Swing UI 設計器 :( 我在設計器中使用多個面板和帶有 1 個父面板的間隔物設計了一些東西。當我添加主面板時,它里面的第一個顯示出來,但其他的不顯示。
框架結構:
我設計的
我得到的
import keeptoo.KGradientPanel;
import javax.swing.*;
public class LogIn extends JFrame{
private KGradientPanel KGradientPanel1; //Automatically added by the designer
private JPanel panel1; //Automatically added by the designer
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("CarbonTec Dashboard");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(1800,1000);
frame.setContentPane(new LogIn().panel1);
frame.setVisible(true);
ImageIcon imageIcon = new ImageIcon("Icon.png");
frame.setIconImage(imageIcon.getImage());
}
}
首先,您需要知道您的類是一個 JFrame,但是在 main 方法中您創建了一個新的 JFrame。
最好有一個具有主要方法的類 Program 。 在這個 main 方法中,您創建了一個新的 LogIn 實例。
Program 類可能如下所示:
public class MainProgram {
public static void main(String[] args) {
LogIn logIn = new LogIn();
}
}
LogIn 類應該如下所示:
import keeptoo.KGradientPanel;
import javax.swing.*;
public class LogIn extends JFrame{
private KGradientPanel KGradientPanel1 = new KGradientPanel(); //Automatically added by the designer
private JPanel panel1 = new JPanel(); //Automatically added by the designer
// This is the constructor.
public LogIn {
setTitle("CarbonTec Dashboard");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setSize(1800,1000);
setContentPane(panel1);
ImageIcon imageIcon = new ImageIcon("Icon.png");
setIconImage(imageIcon.getImage());
// Here you can add the gradient panel to panel1.
panel1.add(KGradientPanel1); // The name should be written in lower case.
setVisible(true);
}
但是不知道為什么需要panel1,可以直接加KGradientPanel。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.