本人妹子一个,对于Java真的不太明白。在这里求教各位大神!拜托了!研究一周还不行!
首先我有两个文件,house.java和RealState.java.
RealState里面有所有房子的信息,包括id,价格,大小,地址。还有其他一些方法。
然后我需要设计一个JApplet。
mainscreen上有三个键,分别是触发三个Panel。
一个panel需要可以让用户添加一整条信息到RealState里,第二个Panel要显示所有的存在RealState里的信息,第三个要调用RealState里的方法。
目前我就只会让三个键出现,以下是我目前的code。
如果我描述的不够清楚拜托私聊!
拜托了!很急!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RealStateApplet extends JFrame implements ActionListener{
private CardLayout cardManager;
private JPanel p1,p2,p3;
private JButton controls[];
private String names[]={"Input a new house","View all houses","Analyze the market"};
public RealStateApplet(){
super("RealStateApplet");
Container container=getContentPane();
p1=new JPanel();
cardManager=new CardLayout();
p1.setLayout(cardManager);
JLabel label1= new JLabel("Input a new house",SwingConstants.CENTER);
JPanel card1= new JPanel();
card1.add(label1);
p1.add(card1,label1.getText());
card1.setLayout(new BorderLayout());
JPanel inputPanel=new JPanel();
inputPanel.setLayout(new GridLayout(4,1));
JTextField textField1 = new JTextField(20);
JLabel label11= new JLabel("House ID:");
inputPanel.add(label11);
inputPanel.add(textField1,FlowLayout.LEFT);
JLabel label12= new JLabel("Price:");
inputPanel.add(label12);
inputPanel.add(new JTextField(20),FlowLayout.LEFT);
JLabel label13= new JLabel("Size:");
inputPanel.add(label13);
inputPanel.add(new JTextField(20),FlowLayout.LEFT);
JLabel label14= new JLabel("Address:");
inputPanel.add(label14);
inputPanel.add(new JTextField(20),FlowLayout.LEFT);
card1.add(inputPanel,BorderLayout.WEST);
JLabel label2= new JLabel("Show all houses",SwingConstants.CENTER);
JPanel card2= new JPanel();
card2.add(label1);
p1.add(card2,label1.getText());
//JTextArea(RealStatePresentationCenter.showAllHouses());
//card2.setLayout(new );
JLabel label3= new JLabel("Analyze the market",SwingConstants.CENTER);
JPanel card3=new JPanel();
card3.add(label3);
p1.add(card3,label3.getText());
JPanel buttons=new JPanel();
buttons.setLayout(new GridLayout(1,3));
controls=new JButton[names.length];
for(int count=0;count<controls.length;count++){
controls[count]=new JButton(names[count]);
controls[count].addActionListener(this);
buttons.add(controls[count]);
}
container.add(buttons, BorderLayout.NORTH);
setSize(450,200);
setVisible(true);
}
public void actionPerformed(ActionEvent event){
if(event.getSource()==controls[0])
cardManager.first(p1);
else if(event.getSource()==controls[1])
cardManager.next(p1);
else if(event.getSource()==controls[2])
cardManager.last(p1);
}
public static void main(String args[]){
RealStateApplet realStateDemo=new RealStateApplet();
realStateDemo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
------解决思路----------------------
你是要3个Panel切换的功能,还是要实现你说的那个复杂的功能?你到底要干嘛没说清楚。
如果是第一个问题就简单了。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
/**
*
* @author zys59三仙半(QQ:597882752)<br>
* 创建时间:2015年7月10日 上午9:46:37
*/
public class PanelSwap extends JFrame {
private static final long serialVersionUID = 7129848483540888535L;
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PanelSwap frame = new PanelSwap();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//定义3个JPanel,这几个JPanel会被按钮控制分别增加到界面上去
private JPanel p1,p2,p3;
private JPanel panel_1;
public PanelSwap() {
//主窗体的基本设置
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
//分别初始化3个JPanel,在JPanel上增加一个JTextField是为了区分
//对于实际项目就是要显示的项目,很可能需要做成一个独立的类
p1 = new JPanel();
JTextField tf_p1 = new JTextField();
tf_p1.setText("这是Panel——1");
p1.add(tf_p1);
//初始化
p2 = new JPanel();
JTextField tf_p2 = new JTextField();
tf_p2.setText("这是Panel——2");
p2.add(tf_p2);
//初始化
p3 = new JPanel();
JTextField tf_p3 = new JTextField();
tf_p3.setText("这是Panel——3");
p3.add(tf_p3);
//这个JPanel用于容纳3个按钮
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
//3个按钮,点击会分别显示对应的JPanel
JButton btnNewButton = new JButton("显示P1");
btnNewButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel_1.removeAll();
panel_1.add(p1, BorderLayout.CENTER);
panel_1.validate();
}
});
panel.add(btnNewButton);
JButton btnNewButton_1 = new JButton("显示P2");
btnNewButton_1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel_1.removeAll();
panel_1.add(p2, BorderLayout.CENTER);
panel_1.validate();
}
});
panel.add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("显示P3");
btnNewButton_2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel_1.removeAll();
panel_1.add(p3, BorderLayout.CENTER);
panel_1.validate();
}
});
panel.add(btnNewButton_2);
//3个JPanel最终会在这个JPanel中显示
panel_1 = new JPanel();
panel_1.setLayout(new BorderLayout());
contentPane.add(panel_1, BorderLayout.CENTER);
}
}