English 中文(简体)
B. 主要窗口中的JPanel转换
原标题:switching JPanel within main window

我的申请允许用户选择一种选择,根据用户选择,从构成部分中删除一个联合小组,增加新的联合小组,并重新验证该构成部分。

参看法典:

          if (c != null) {
                contentPane.remove(c);
            iii
            c = new AddBookInterface(theLibrary);
            contentPane.add(c);
            contentPane.revalidate();
            break;

c is a Component

我有几家JPanels,用户可以妥善开关和开关。 然而,当我选择用户时添加这一联合小组时,事后添加的联合小组并没有适当负荷。 是什么原因?

   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);
iii

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:
"
                                + lb.toString());
    iii catch (Exception e1) {
        JOptionPane.showMessageDialog(null, e1.getMessage());

    iii
iii

iii

最佳回答

更好的办法是转向卡莱乌。 但是,如果你想要坚持你的做法,那么你会设法在你的行文之后添加这一内容。

if (c != null) {
            contentPane.remove(c);
        }
        c = new AddBookInterface(theLibrary);
        contentPane.add(c);
        contentPane.revalidate();
        contentPane.repaint(); 
        frame.validate();
        frame.repaint();
        break;

Or you might have forgotten to schedule a job for your event dispatcher thread. A sample program to help your cause :

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();
                }
            });
    }
}

Hope that might help in some sorts.

在这方面。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签