java关于Borderlayout布局的问题
请各位大哥帮看下下面的代码,我想remove掉原来的一个jpanel,然后在在它的位置上显示另一个jpanel,不知道这段代码为什么无法实现呢?帮帮忙,谢谢importj...
请各位大哥帮看下下面的代码,我想remove掉原来的一个jpanel,然后在在它的位置上显示另一个jpanel,不知道这段代码为什么无法实现呢?帮帮忙,谢谢
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame1
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jButton1.setText("测试");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextField1.setText("jTextField1");
contentPane.add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel1.add(jButton1);
contentPane.add(jPanel2, java.awt.BorderLayout.WEST);
jPanel2.add(jTextField1);
}
public void jButton1_actionPerformed(ActionEvent e) {
panel p=new panel();
contentPane.remove(jPanel2);
contentPane.add(p, java.awt.BorderLayout.WEST);
this.show();
}
}
class Frame1_jButton1_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
} 展开
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Frame1
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jButton1.setText("测试");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextField1.setText("jTextField1");
contentPane.add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel1.add(jButton1);
contentPane.add(jPanel2, java.awt.BorderLayout.WEST);
jPanel2.add(jTextField1);
}
public void jButton1_actionPerformed(ActionEvent e) {
panel p=new panel();
contentPane.remove(jPanel2);
contentPane.add(p, java.awt.BorderLayout.WEST);
this.show();
}
}
class Frame1_jButton1_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
} 展开
3个回答
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jButton1.setText("测试");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextField1.setText("jTextField1");
contentPane.add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel1.add(jButton1);
contentPane.add(jPanel2, java.awt.BorderLayout.WEST);
jPanel2.add(jTextField1);
}
public void jButton1_actionPerformed(ActionEvent e) {
Panel p=new Panel();
contentPane.remove(jPanel2);
contentPane.add(p, java.awt.BorderLayout.WEST);
this.show();
}
}
class Frame1_jButton1_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Frame1 extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
public Frame1() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(borderLayout1);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jPanel1.setBorder(BorderFactory.createEtchedBorder());
jPanel2.setBorder(BorderFactory.createEtchedBorder());
jButton1.setText("测试");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
jTextField1.setText("jTextField1");
contentPane.add(jPanel1, java.awt.BorderLayout.NORTH);
jPanel1.add(jButton1);
contentPane.add(jPanel2, java.awt.BorderLayout.WEST);
jPanel2.add(jTextField1);
}
public void jButton1_actionPerformed(ActionEvent e) {
Panel p=new Panel();
contentPane.remove(jPanel2);
contentPane.add(p, java.awt.BorderLayout.WEST);
this.show();
}
}
class Frame1_jButton1_actionAdapter
implements ActionListener {
private Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
除了上面两位提到的panel p=new panel(); 有问题,
可改为JPanel p=new JPanel();外
还有有一个main()方法来供测试。
public static void main(String args[])
{
Frame1 test=new Frame1();
test.show();
}
可改为JPanel p=new JPanel();外
还有有一个main()方法来供测试。
public static void main(String args[])
{
Frame1 test=new Frame1();
test.show();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
panel p=new panel();
你的这个panel应该改成Panel
你的这个panel应该改成Panel
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询