繁体   English   中英

除非我调整JFrame的大小,否则JTextArea不会更新

[英]JTextArea Won't Update Unless I Resize JFrame

每当我运行程序时,我的JTextArea都不会遵循我给定的尺寸,但是如果我调整JFrame的大小,它会更新并将其大小设置为我放置的大小。

有什么问题

    public ControlPanel() {
    // create our list of players
    list = new JList(model);

    // create our scroll panes
    userspane = new JScrollPane(list);
    consolepane = new JScrollPane(console);

    // set sizes
    userspane.setSize(100, 500);
    jta.setSize(100, 500);
    list.setSize(100, 500);
    consolepane.setSize(100, 500);
    console.setSize(100, 500);

    // add to panel
    panel.add(userspane, BorderLayout.CENTER);
    panel.add(kick);
    panel.add(ban);
    panel.add(info);
    panel.add(consolepane, BorderLayout.CENTER);

    // set frame properties
    setTitle("RuneShadows CP");
    setSize(280, 400);
    //setResizable(false);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setContentPane(panel);
    setVisible(true);
}

不要将大小设置为任何大小。

  • 对于JTextArea ,可以使用构造函数JTextArea(int rows, int charSpaces)
  • 只需将JFrame pack() ,它将尊重内部组件的所有首选大小。
  • 另外,只需将面板添加即可,而不是在面板上设置内容窗格。 当调用pack()时,这将尊重面板的首选大小

我不完全确定哪个变量是什么(或您想要的大小),所以我假定了文本区域以及其他区域。 在这个示例中,我刚刚使用了我提到的JTextArea构造函数并pack

未设置尺寸进行 编辑

import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ControlPanel extends JFrame {

    JScrollPane userspane;
    JList list;
    DefaultListModel model = new DefaultListModel();
    JScrollPane consolepane;
    JTextArea console = new JTextArea(20, 50);
    JTextArea jta = new JTextArea(6, 50);
    JPanel panel = new JPanel();

    JButton kick = new JButton("Kick");
    JButton ban = new JButton("Ban");
    JButton info = new JButton("Info");

    public ControlPanel() {
        // create our list of players
        list = new JList(model);

        // create our scroll panes
        userspane = new JScrollPane(list);
        consolepane = new JScrollPane(console);

        // add to panel
        panel.add(userspane, BorderLayout.CENTER);
        panel.add(kick);
        panel.add(ban);
        panel.add(info);
        panel.add(consolepane, BorderLayout.CENTER);

        add(panel);
        pack();
        setTitle("RuneShadows CP");
        //setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new ControlPanel();
            }
        });
    }
}

更新 -定位

还要记住,使用BorderLayout您需要为添加的每个组件指定一个位置,否则它将默认为CENTER且每个位置只有一个组件。 我注意到您尝试向CENTER添加两个组件

import java.awt.BorderLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ControlPanel extends JFrame {

    JScrollPane userspane;
    JList list;
    DefaultListModel model = new DefaultListModel();
    JScrollPane consolepane;
    JTextArea console = new JTextArea(20, 50);
    JTextArea jta = new JTextArea(6, 50);
    JPanel panel = new JPanel(new BorderLayout());

    JButton kick = new JButton("Kick");
    JButton ban = new JButton("Ban");
    JButton info = new JButton("Info");

    public ControlPanel() {
        // create our list of players
        list = new JList(model);

        // create our scroll panes
        userspane = new JScrollPane(list);
        consolepane = new JScrollPane(console);

        // add to panel
        panel.add(userspane, BorderLayout.SOUTH);
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(kick);
        buttonPanel.add(ban);
        buttonPanel.add(info);
        panel.add(buttonPanel, BorderLayout.CENTER);
        panel.add(consolepane, BorderLayout.NORTH);

        add(panel);
        pack();
        setTitle("RuneShadows CP");
        //setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                new ControlPanel();
            }
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM