【发布时间】:2022-08-03 00:14:20
【问题描述】:
我在 Java Swing 中创建了一个小型 GUI,但我面临标签位置的一个小问题。 我需要在框架的顶部中心显示标签,但在我的代码中,即使我添加了设置边界,它仍然显示在错误的位置。如何在中心显示标签?
该面板也没有显示在我的屏幕上。不知道为什么。
我的代码
public class GuiInterface {
public void GUI()
{
// Frame
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame(\"Fault Localization\");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Fonts
Font f2 = new Font(Font.SANS_SERIF, Font.BOLD, 20);
Font f3 = new Font(Font.SANS_SERIF, Font.PLAIN, 15);
//Components
JPanel mPanel=new JPanel();
mPanel.setBackground(Color.lightGray);
mPanel.setLayout(new BorderLayout());
JButton jb1 = new JButton(\"Here\");
// Text Area
JTextArea fTextArea=new JTextArea();
//fTextArea.setBounds(60,150, 400,400);
fTextArea.setMargin(new Insets(3,3,3,3));
fTextArea.setEditable ( false ); // set textArea non-editable
JScrollPane scroll = new JScrollPane(fTextArea);
JLabel tittle= new JLabel(\"Fault\");
// tittle.setBounds(30,30, 400,20);
tittle.setFont(f2);
//Adding the components to the panel
mPanel.add(jb1, BorderLayout.SOUTH);
// Frame Settings
frame.add(mPanel);
frame.add(tittle);
frame.pack();
frame.setVisible(true);
frame.setSize(800,800);
}
}
标签: java swing user-interface jframe jpanel