无法更新面板上的UI

时间:2013-12-02 16:13:57

标签: java swing user-interface

如果您查看下面的代码,我试图做的是修改我的addExpense()方法以生成一个新的ExpensePanel并将其添加到我的一个面板(面板3)和向量中并更新UI小组3并更新统计数据。

然而,经过无数次的修改,我似乎无法让它发挥作用。任何人都可以提供一些见解吗?

    import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.UIManager;

class Expenses extends JFrame implements ActionListener{

    ExpensesPanel widget;
    public String n;
    public String d;
    public int c;
    public int e;

    public Expenses(){

        // Set Dimension, size, and close operations
        setSize(400,400);
        setTitle("Expenses");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(true);
        setLocationRelativeTo(null);

        // Set the look and feel of the application to that if the OS
        try{
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e){
            System.out.print(e);
        }

        // Add Panel 1, or our main panel
        JPanel p1 = new JPanel();
        getContentPane().add(p1);
        p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));

        // Add our second panel, panel 2 with a grid layout
        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(4,2));
        p1.add(p2);

        // Add our labels
        JLabel label1 = new JLabel("Name: ", JLabel.LEFT);
        JTextField name = new JTextField(JTextField.EAST);
        n = name.getText();
        JLabel label2 = new JLabel("Date: ", JLabel.LEFT);
        JTextField date = new JTextField(JTextField.EAST);
        d = date.getText();
        JLabel label3 = new JLabel("Cost: ", JLabel.LEFT);
        JTextField cost = new JTextField(JTextField.EAST);
        p2.add(label1);
        p2.add(name);
        p2.add(label2);
        p2.add(date);
        p2.add(label3);
        p2.add(cost);
        // Add our buttons
        JButton b1 = new JButton("Add");
        p2.add(b1);
        JButton b2 = new JButton("Clear");
        p2.add(b2);
        // Set the preferred size of our windows
        p2.setMaximumSize(new Dimension(Integer.MAX_VALUE, p2.getPreferredSize().height));

        // Panel 3, this will hold the expenses panel
        final JPanel p3 = new JPanel();
        p3.setLayout(new BoxLayout(p3, BoxLayout.Y_AXIS));
        JScrollPane scroll = new JScrollPane(p3);
        p1.add(scroll);

        // Panel 4, with a 2x2 Grid
        JPanel p4 = new JPanel();
        p4.setLayout(new GridLayout(2,2));
        p1.add(p4);
        // Add labels, and ensure they are not editable
        JLabel p4l1 = new JLabel("Total", JLabel.LEFT);
        JTextField p4t1 = new JTextField(JTextField.EAST);
        p4t1.setEditable(false);
        JLabel p4l2 = new JLabel("Average", JLabel.LEFT);
        JTextField p4t2 = new JTextField(JTextField.EAST);
        p4t2.setEditable(false);
        p4.add(p4l1);
        p4.add(p4t1);
        p4.add(p4l2);
        p4.add(p4t2);
        // Set the preferred size to fit the rest of the window
        p4.setMaximumSize(new Dimension(Integer.MAX_VALUE, p2.getPreferredSize().height));

        // Action listeners for both buttons
        // Action listner for button 1
        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button 1 Clicked");
                addExpense(p3);
            }

        });

        // Action Listener for button 2
        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button 2 Clicked");
            }

        });

    }

    public void addExpense(JPanel p3){
        Vector expenseHolder = new Vector();
        ExpensesPanel e = new ExpensesPanel(n, d, 2, 1);
        p3.add(e);
        expenseHolder.add(p3);
        p3.revalidate();
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }

    public static void main(String[] args){
        // Main method
        Expenses e = new Expenses();
        // Set the application to be visible
        e.setVisible(true);
    }
}

class ExpensesPanel extends JPanel{

    public String n;
    public String d;
    public int c;
    public int e;

    public ExpensesPanel(String name, String date, int cost, int expense){
        n = name; d = date; c = cost; e = expense;

        // Create new Panel and set it on horizontal axis
        JPanel exp = new JPanel();
        exp.setLayout(new BoxLayout(exp, BoxLayout.X_AXIS));
        Box horizontalBox;

        // Labels
        JLabel newName = new JLabel("Name: ", JLabel.CENTER);
        JLabel newDate = new JLabel("Date", JLabel.CENTER);
        JLabel newCost = new JLabel("Cost: ", JLabel.CENTER);
        JLabel newExp = new JLabel("Expense: ", JLabel.CENTER);
        exp.add(newName, Box.createHorizontalGlue());
        exp.add(newDate, Box.createHorizontalGlue());
        exp.add(newCost, Box.createHorizontalGlue());
        exp.add(newExp, Box.createHorizontalGlue());

    }

    // Override default paint component
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setBackground(Color.BLUE);
    }
}

2 个答案:

答案 0 :(得分:1)

你的问题是你的ExpensePanel类有两个 JPanels,一个是你要调用的组件,exp,另一个是this或JPanel,它是该类的对象,你没有添加任何组件。那么你将第二个JPanel添加到GUI中,这样就不会显示任何内容。另请注意,exp在构造函数中声明,因此其范围仅限于构造函数。

一种可能的解决方案:摆脱 exp 并将所有内容添加到this。请注意,您可以使用this.表达式,例如this.add(someComponent)明确表明您的意图,但这是不必要的,并添加额外的单词,在这种情况下几乎没有任何好处,所以我通常避免它

即,将构造函数更改为:

public ExpensesPanel(String name, String date, int cost, int expense) {
  n = name;
  d = date;
  c = cost;
  e = expense;

  // Create new Panel and set it on horizontal axis
  // JPanel exp = new JPanel();
  setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
  Box horizontalBox;

  // Labels
  JLabel newName = new JLabel("Name: ", JLabel.CENTER);
  JLabel newDate = new JLabel("Date", JLabel.CENTER);
  JLabel newCost = new JLabel("Cost: ", JLabel.CENTER);
  JLabel newExp = new JLabel("Expense: ", JLabel.CENTER);
  add(newName, Box.createHorizontalGlue());
  add(newDate, Box.createHorizontalGlue());
  add(newCost, Box.createHorizontalGlue());
  add(newExp, Box.createHorizontalGlue());
}

另一种可能的解决方案是保留exp JPanel,但是使用getter方法将其提升为实例字段,然后不要让ExpensesPanel扩展JPanel。两者都可以工作,后者有一些优点,应该在以后讨论(关于使用组合而不是继承的优点)。


顺便说一句,这不会做任何事情:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.BLUE);
}

因为在更改其Color状态后,您永远不会使用g2d绘制或清除(clearRect(...)实际上)。

更好,更容易调用构造函数:

setBackground(Color.blue);

问题接下来,这是错误的:

add(newCost, Box.createHorizontalGlue());

那不是你添加胶水的方式。请检查这方面的教程和API - 您正在进行大量的猜测工作,这些工作永远无效。

答案 1 :(得分:1)

除了@Hovercraft Full Of Eels的答案之外,您还应该通过以下方式对GUI组件进行更改...

 EventQueue.invokeLater(new Runnable(){
  public void run()
  { 
    //make gui change here
  }
 });

这会从事件调度线程(EDT)更新它。