怎样在JAVA窗口中切换_java – 在主窗口中切换JPanel

我有一个应用程序,允许用户选择一个选项,并根据用户选择从组件中删除JPanel,添加新的JPanel并重新验证组件

看代码:

if (c != null) {

contentPane.remove(c);

}

c = new AddBookInterface(theLibrary);

contentPane.add(c);

contentPane.revalidate();

break;

c是一个组件

我有几个JPanels,用户可以切换,交换机正常工作.但是,当我在用户选择时添加此JPanel时,之后添加的JPanel无法正确加载.是什么造成的?

public class RemoveBookInterface extends JPanel {

private Library theLibrary;

public RemoveBookInterface(Library theLibrary) {

this.theLibrary = theLibrary;

setSize(400, 400);

setLayout(new BorderLayout());

setVisible(true);

removeBook(theLibrary);

}

public void removeBook(Library theLibrary) {

// prompt user for book id of book to remove

Long ID = Long

.parseLong(JOptionPane

.showInputDialog("Enter the library book ID for the book you want to remove"));

try {

// get library book info and store it to display in message

LibraryBook lb = theLibrary.getInventory().findLibraryBook(ID);

// remove book

theLibrary.removeBook(ID);

// display message indicating book was removed

JOptionPane

.showMessageDialog(

null,

"The following library book was removed:\n"

+ lb.toString());

} catch (Exception e1) {

JOptionPane.showMessageDialog(null, e1.getMessage());

}

}

}

解决方法:

更好的方法是转移到CardLayout.但如果你想坚持你的方法,那么尝试在你的线后添加

if (c != null) {

contentPane.remove(c);

}

c = new AddBookInterface(theLibrary);

contentPane.add(c);

contentPane.revalidate();

contentPane.repaint();

frame.validate();

frame.repaint();

break;

或者您可能忘记为事件调度程序线程安排作业.

帮助您的事业的示例程序:

import java.awt.event.*;

import javax.swing.*;

public class TwoPanelTest implements ActionListener

{

private JFrame frame;

private JPanel panel1;

private JPanel panel2;

private JButton button1;

private JButton button2;

private JLabel label1;

private JLabel label2;

private JTextField tfield1;

private JTextField tfield2;

public TwoPanelTest()

{

frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

panel1 = new JPanel();

panel2 = new JPanel();

label1 = new JLabel("This is Label 1");

label2 = new JLabel("This is Label 2");

button1 = new JButton("BUTTON 1");

button2 = new JButton("BUTTON 2");

button1.addActionListener(this);

button2.addActionListener(this);

tfield1 = new JTextField(20);

tfield2 = new JTextField(20);

panel1.add(label1);

panel1.add(button1);

panel1.add(tfield1);

panel2.add(label2);

panel2.add(button2);

panel2.add(tfield2);

tfield1.setText("MY TEXT WILL CHANGE.");

frame.setContentPane(panel1);

frame.pack();

frame.setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

JButton button = (JButton)ae.getSource();

if (button == button1)

{

frame.remove(panel1);

frame.setContentPane(panel2);

tfield2.setText("TEXTFIELD 2");

frame.validate();

frame.repaint();

}

else if (button == button2)

{

frame.remove(panel2);

frame.setContentPane(panel1);

tfield1.setText("TEXTFIELD 1");

frame.validate();

frame.repaint();

}

}

public static void main(String[] args)

{

// Here Event Dispatcher thread is responsible for

// calling the function which creates and displays your GUI

// or it itself contains the code for creating and displaying

// the GUI, to remove hickups experienced while updating the

// GUI on the run.

SwingUtilities.invokeLater(new Runnable()

{

public void run()

{

new TwoPanelTest();

}

});

}

}

希望在某些方面可能会有所帮助.

问候.

标签:joptionpane,java,components,swing,jpanel

来源: https://codeday.me/bug/20191002/1843709.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值