我正在尝试在JFrame中显示JPanel。 Jrame有效,但我无法显示JPanel。
我全班同学今天花了一个小时。包括老师。不运气。 Eclipse说没有错误有人可以提醒我错误吗?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// make a JFrame and bits
public class MySystemGUI extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L;
private JFrame myFrame;
private JTextField LLName, LLAddress, LLPhone, LLbankDeets;
private JButton sub1;
private JLabel LLNameT, LLAddressT, LLPhoneT, LLbankDeetsT;
private JPanel LLJP()
{
JPanel JP1 = new JPanel();
LLNameT = new JLabel ("Enter Landlord name");
LLName = new JTextField(30);
LLAddressT = new JLabel ("Enter Landlord Address ");
LLAddress = new JTextField(40);
LLPhoneT = new JLabel ("Enter Landlod Phone No.");
LLPhone = new JTextField(10);
LLbankDeetsT = new JLabel ("Enter Landlod Bank details");
LLbankDeets = new JTextField(10);
sub1 = new JButton("Submit");
JP1.add(LLNameT);
JP1.add(LLName);
JP1.add(LLAddressT);
JP1.add(LLAddress);
JP1.add(LLPhoneT);
JP1.add(LLPhone);
JP1.add(LLbankDeetsT );
JP1.add(LLbankDeets);
JP1.add(sub1);
//myFrame.add(JP1 );
return JP1;
}
// Set up frame
public MySystemGUI()
{
myFrame = new JFrame ();
JPanel myPanel = LLJP();
myFrame.add(myPanel,"South");
this.setLayout(new GridBagLayout());
this.setSize(700, 500);
this.setTitle("My System GUI");
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.blue);
}
//run this bitch
public static void main (String[] args)
{
new MySystemGUI();
}
@Override
public void actionPerformed(ActionEvent e)
{
}
}
答案 0 :(得分:4)
您有一个JFrame成员
private JFrame myFrame;
这是您要添加组件的内容。
myFrame.add(myPanel,"South");
但你将setVisible设置为类框架
public class MySystemGUI extends JFrame
...
this.setVisible(true); // `this` is class frame, not `myFrame`
取出所有this.setXxx
并执行myFrame.setXxx
,然后取出extends JFrame
public class MySystemGUI implements ActionListener {
...
public MySystemGUI() {
...
myFrame.setLayout(new GridBagLayout());
myFrame.setSize(700, 500);
myFrame.setTitle("My System GUI");
myFrame.setVisible(true);
...
}
}
其他笔记
myFrame.add(myPanel,"South");
- " South"没有关系,仅适用于BorderLayout。您将布局设置为GridBaglayout。
this.setBackground(Color.blue);
您希望在框架可见后设置背景吗?
应在Event Dispatch Thread上运行/启动Swing程序。见Initial Threads。基本上,您可以将实例化包装在SwingUtilities.invokeLater
public static void main (String[] args) {
SwingUtilities.invokeLater(new Runnable(){
public void run() {
new MySystemGUI();
}
});
}
<强>更新强>
您还希望为包含标签和字段的面板使用不同的布局管理器。使用FlowLayout(默认为JPanel),连续添加eveerytyhing。花一些时间在Laying out Components Withing a Container
学习布局管理器以下是使用GridLayout
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MySystemGUI implements ActionListener {
private static final long serialVersionUID = 1L;
private JFrame myFrame;
private JTextField LLName, LLAddress, LLPhone, LLbankDeets;
private JButton sub1;
private JLabel LLNameT, LLAddressT, LLPhoneT, LLbankDeetsT;
private JPanel LLJP() {
JPanel JP1 = new JPanel(new GridLayout(0, 2));
LLNameT = new JLabel("Enter Landlord name");
LLName = new JTextField(30);
LLAddressT = new JLabel("Enter Landlord Address ");
LLAddress = new JTextField(40);
LLPhoneT = new JLabel("Enter Landlod Phone No.");
LLPhone = new JTextField(10);
LLbankDeetsT = new JLabel("Enter Landlod Bank details");
LLbankDeets = new JTextField(10);
sub1 = new JButton("Submit");
JP1.add(LLNameT);
JP1.add(LLName);
JP1.add(LLAddressT);
JP1.add(LLAddress);
JP1.add(LLPhoneT);
JP1.add(LLPhone);
JP1.add(LLbankDeetsT);
JP1.add(LLbankDeets);
JP1.add(sub1);
return JP1;
}
public MySystemGUI() {
myFrame = new JFrame();
JPanel myPanel = LLJP();
myFrame.add(myPanel);
myFrame.setTitle("My System GUI");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setBackground(Color.blue);
myFrame.pack();
myFrame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MySystemGUI();
}
});
}
@Override
public void actionPerformed(ActionEvent e){}
}
答案 1 :(得分:2)
您有一个隐藏您的课程的本地框架,实际上是JFrame
:
public class MySystemGUI extends JFrame implements ActionListener { // class signature
...
public MySystemGUI() {
...
myFrame = new JFrame (); // local variable
...
myFrame.add(myPanel,"South");
...
}
...
}
您的面板已添加到此本地框架而非您的班级,因此无法显示。您可以删除此局部变量并使用this.add(...)
添加面板,或者避免扩展JFrame
并使用局部变量。最后一个应用是首选:
我全班同学今天花了一个小时。包括老师。不 运气。
找到另一位老师。