java GUI 运行显示不出结果~~谢谢..
import java.awt.*;
import java.awt.event.ActionListener;
import javax.faces.event.ActionEvent;
import javax.swing.*;
public class Chatroom extends JFrame implements ActionListener {
TextArea Area= new TextArea();
BorderLayout borderlayout1 = new BorderLayout();
JPanel panel1=new JPanel();
GridLayout gridlayout1 = new GridLayout();
JLabel Lab1=new JLabel();
JTextField tf1= new JTextField(10);
JLabel Lab2=new JLabel();
JPanel panel2=new JPanel();
FlowLayout flowlayout1 = new FlowLayout();
JButton send=new JButton();
JButton exit=new JButton();
public void crFrame(){
this.setSize(200, 200);
send.addActionListener(this);
exit.addActionListener(this);
this.getContentPane().add(panel1,BorderLayout.CENTER);
tf1.setText("");
Lab2 = new JLabel("要说的话");
Lab2.setHorizontalAlignment(SwingConstants.CENTER);
panel2.setLayout(flowlayout1);
send = new JButton("发送");
exit = new JButton("退出");
panel1.add(Lab1);
Lab1.setHorizontalAlignment(SwingConstants.CENTER);
panel1.add(Area);
panel1.add(Lab2);
panel1.add(tf1);
this.getContentPane().add(panel2,BorderLayout.CENTER);
panel2.add(send);
panel2.add(exit);
Lab1 = new JLabel("聊天内容");
panel1.setLayout(gridlayout1);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==exit)
System.exit(0);
else if(e.getSource()==send){
String str="tom说:"+tf1.getText().trim();
tf1.setText("");
System.out.println(str);
Area.setText(Area.getText()+str+'\n');
}
}
public static void main(String[] args){
new Chatroom().crFrame();
}
public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO Auto-generated method stub
}
}
我运行的界面是这样的: 展开
/**
*备注:将panel1的布局方式改为BoxLayout,fix了一些其他细节,可参考,并附上运行后的图片。
*/
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
public class Chatroom extends JFrame implements ActionListener {
TextArea Area = new TextArea();
BorderLayout borderlayout1 = new BorderLayout();
JPanel panel1 = new JPanel();
GridLayout gridlayout1 = new GridLayout(2, 2);
JLabel Lab1 = new JLabel();
JTextField tf1 = new JTextField(10);
JLabel Lab2 = new JLabel();
JPanel panel2 = new JPanel();
FlowLayout flowlayout1 = new FlowLayout();
JButton send = new JButton();
JButton exit = new JButton();
public void crFrame() {
// this.setSize(200, 300);
this.setTitle("Chatroom");
send = new JButton("发送");
exit = new JButton("退出");
send.addActionListener(this);
exit.addActionListener(this);
tf1.setText("");
tf1.setBounds(new Rectangle(0, 0, 150, 120));
Lab2 = new JLabel("要说的话");
Lab2.setHorizontalAlignment(SwingConstants.CENTER);
// panel1.setLayout(gridlayout1);
panel1.setLayout(new BoxLayout(panel1,BoxLayout.Y_AXIS));
panel2.setLayout(flowlayout1);
Lab1 = new JLabel("聊天内容");
Lab1.setHorizontalAlignment(SwingConstants.CENTER);
panel1.add(Lab1);
panel1.add(Area);
panel1.add(Lab2);
panel1.add(tf1);
panel2.add(send);
panel2.add(exit);
this.getContentPane().add(panel1, BorderLayout.NORTH);
this.getContentPane().add(panel2, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit)
System.exit(0);
else if (e.getSource() == send) {
String str = "tom说:" + tf1.getText().trim();
tf1.setText("");
System.out.println(str);
Area.setText(Area.getText() + str + '\n');
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}
new Chatroom().crFrame();
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.*;
public class Chatroom extends JFrame implements ActionListener {
TextArea Area= new TextArea(30,30);
BorderLayout borderlayout1 = new BorderLayout();
JPanel panel1=new JPanel();
GridLayout gridlayout1 = new GridLayout();
JLabel Lab1=new JLabel();
JTextField tf1= new JTextField(10);
JLabel Lab2=new JLabel();
JPanel panel2=new JPanel();
FlowLayout flowlayout1 = new FlowLayout();
JButton send=new JButton();
JButton exit=new JButton();
public void crFrame(){
this.setSize(200, 200);
send.addActionListener(this);
exit.addActionListener(this);
this.getContentPane().add(panel1,BorderLayout.CENTER);
tf1.setText("");
Lab2 = new JLabel("要说的话");
Lab2.setHorizontalAlignment(SwingConstants.CENTER);
panel2.setLayout(flowlayout1);
send = new JButton("发送");
exit = new JButton("退出");
panel1.add(Lab1);
//Lab1.setHorizontalAlignment(SwingConstants.CENTER);
panel1.add(Area);
panel1.add(Lab2);
panel1.add(tf1);
//this.getContentPane().add(panel2,BorderLayout.CENTER);
panel2.add(send);
panel2.add(exit);
Lab1 = new JLabel("聊天内容");
panel1.setLayout(gridlayout1);
this.getContentPane().add(panel1,BorderLayout.WEST);
this.getContentPane().add(panel2,BorderLayout.EAST);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==exit)
System.exit(0);
else if(e.getSource()==send){
String str="tom说:"+tf1.getText().trim();
tf1.setText("");
System.out.println(str);
Area.setText(Area.getText()+str+'\n');
}
}
public static void main(String[] args){
new Chatroom().crFrame();
}
/*public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO Auto-generated method stub
}*/
}
你的问题是因为你的两个panel之间一个挡住了另外一个,而且我现在帮你显示的界面布局还是很难看,只是帮你解决了只能显示一个panel的问题,推荐你看看关于布局的内容,相信你肯定会设计出好看的界面