【问题标题】:JPanel doesnt show components after the second button click单击第二个按钮后,JPanel 不显示组件
【发布时间】:2014-05-29 18:27:55
【问题描述】:

单击按钮时,我想将 JTextPane 组件放入带有 GridBagLayout 的 JPanel 中。 我的代码在第一次单击按钮时工作正常,但之后,不会显示下一个组件。

这是我的代码:

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

public class RefreshPanel {

private JFrame frame = new JFrame();
private JPanel panel = new JPanel();
private JTextPane [] textPane;// = new JTextPane[1];
private JScrollPane scrollbar;
private ArrayList arrayList = new ArrayList();
private JButton newItem = new JButton("new");
private int counter=0;
private GridBagLayout gbl = new GridBagLayout();

RefreshPanel() {

    scrollbar = new JScrollPane(panel);

    panel.setBackground(Color.WHITE);
    panel.setLayout(gbl);                       

    addButtonListener();        

    createFrame();
} //constructor

public void addButtonListener() {
    newItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            arrayList.add("data");
            textPane = generateTextPane(arrayList.size(), arrayList);
            System.out.println(textPane.length);
            for(int i=0;i<textPane.length;i++) {
                System.out.println(textPane[i].getText());
                addComponent(panel, gbl, textPane[i], 0, counter, 1, 1,1,1);
                panel.revalidate();
            }       
        }
    });
}

private JTextPane[] generateTextPane(int arraySize, ArrayList arrayList) {
    JTextPane [] textPane = new JTextPane[arraySize];
    for(int i=0;i<textPane.length;i++) {
        textPane[i]=new JTextPane();    
        textPane[i].setText((String) arrayList.get(i));
    }
    return textPane;
}

public void addComponent(Container cont, 
         GridBagLayout gbl, 
         Component c,
         int x, int y,
         int width, int height,
         double weightx, double weighty) {

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = x; gbc.gridy = y;
    gbc.gridwidth = width; gbc.gridheight = height;
    gbc.weightx = weightx; gbc.weighty = weighty;
    gbl.setConstraints( c, gbc );
    cont.add( c );
}
public void createFrame() {
    //frame.getContentPane().setLayout(new FlowLayout());
    frame.add(scrollbar, BorderLayout.CENTER);
    frame.add(newItem, BorderLayout.EAST);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(new Dimension(300,300));
    frame.setVisible(true);
}
public static void main(String [] args) {
    new RefreshPanel();
}       
}

【问题讨论】:

  • 我很确定应该从 Swing 事件调度线程调用构造函数中的代码。不确定它是否完全相关,但这是我会尝试的。查看docs.oracle.com/javase/7/docs/api/javax/swing/…
  • 如果你开始阅读 Oracle 教程,你应该继续阅读 How to use GridBagLayout 那里你会发现一个很好的例子如何使用 GBC。您必须更改您的 GridBagConstraints,到目前为止,您添加每个新的 JTextPane 具有相同的约束。
  • @Patrick:你是对的!我真的忘了更改 -grid-bagconstraints addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 1);** 而不是 **addComponent(panel, gbl, textPane [i], 0, 计数器, 1, 1, 1, 1);
  • @Patrick 你们中的一个应该发布答案。

标签: java swing jpanel jbutton jtextpane


【解决方案1】:

帕特里克:你是对的!我真的忘了改变 gridbagconstraints

addComponent(panel, gbl, textPane[i], 0, i, 1, 1, 1, 1);

而不是

addComponent(panel, gbl, textPane[i], 0, counter, 1, 1, 1, 1);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-26
    相关资源
    最近更新 更多