gpt4 book ai didi

Java:使 JTextArea 可滚动

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:09:11 25 4
gpt4 key购买 nike

这是我的第一篇文章,如果我没有正确遵守规则,请原谅我。

我正在尝试用 Java 做一些相对简单的事情。我想让 JTextArea 可滚动。我知道之前有人问过这个问题(Java :Add scroll into text area)。但是,当我通过将 JTextArea 添加到 JScrollPane 来遵循此示例时,我的 JTextArea 不会变得可滚动。我错过了什么?

这是我的代码:

    import ...;

public class MyControlPanel extends JPanel {

//Declare variables
private JComboBox accountsBox;
private String[] accType = {"Current Account", "Savings Account"};
private JLabel selAccType, initDeposit, logLabel, simLabel;
private JTextField depositText;
private JTextArea log;
private JScrollPane scroll;
private JButton createAccount, start, stop;
private JPanel panel1, panel2, panel3, panel4, panel5, panel6;
private Timer timer;
private MyAccount theAccount;
private Random randNum1, randNum2;
private DecimalFormat df;

//Constructor
public MyControlPanel() {

//Create instances:
selAccType = new JLabel("Please select account type: "); //JLabel
initDeposit = new JLabel("Input initial deposit: ");
logLabel = new JLabel("Log:");
simLabel = new JLabel();
accountsBox = new JComboBox(accType); //JComboBox
depositText = new JTextField("0"); // JTextField
log = new JTextArea(); //JTextArea
scroll = new JScrollPane(log); //JScrollPane
createAccount = new JButton("Create Account"); //JButton
start = new JButton("Start");
stop = new JButton("Stop");
panel1 = new JPanel(); //JPanel
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel6 = new JPanel();
timer = new Timer(); //Timer
df = new DecimalFormat("#.00");

//Add ActionListeners
createAccount.addActionListener(new ActionListener() {...});

start.addActionListener(new ActionListener() {...});

stop.addActionListener(new ActionListener() {...});

//Set JTextField size
depositText.setColumns(5);

//Set JTextArea size
log.setPreferredSize(new Dimension(780, 150));

//Set panel size
panel1.setPreferredSize(new Dimension(500, 500));
panel2.setPreferredSize(new Dimension(800, 50));
panel3.setPreferredSize(new Dimension(500, 50));
panel4.setPreferredSize(new Dimension(500, 50));
panel5.setPreferredSize(new Dimension(800, 25));
panel6.setPreferredSize(new Dimension(800, 200));

//Set layout in panel5 to align left
panel6.setLayout(new FlowLayout(FlowLayout.LEFT));

//Add components to each panel
addPanels();

//Place objects in the framed window
this.add(panel1);
this.add(panel2);
this.add(panel3);
this.add(panel4);
this.add(panel5);
this.add(panel6);
}

public void addPanels() {...}

public void removePanels() {...}

}

最佳答案

你需要改变:

log.setPreferredSize(new Dimension(780, 150));

到:

scroll.setPreferredSize(new Dimension(780, 150));

关于Java:使 JTextArea 可滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33829502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com