gpt4 book ai didi

java - JScrollPane 不可见

转载 作者:行者123 更新时间:2023-12-01 10:00:41 25 4
gpt4 key购买 nike

滚动 Pane 在此处不可见。我知道问题是setlayout(null)。我可以写什么来代替这个?

labelsend.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e3){
framelogin.setVisible(false);
JFrame framesend = new JFrame();
framesend.setSize(500,500);

JPanel panelsend = new JPanel();
panelsend.setLayout(null);

JLabel labelsendname = new JLabel("Name: ");
labelsendname.setBounds(20,20,50,10);
panelsend.add(labelsendname);

JTextField textsendname = new JTextField();
textsendname.setBounds(60, 15, 400, 18);
panelsend.add(textsendname);

JLabel labelmessagesend = new JLabel("Message: ");
labelmessagesend.setBounds(1,50,80,14);
panelsend.add(labelmessagesend);

JTextArea textmessagesend = new JTextArea(5,10);
textmessagesend.setBounds(60,50,400,300);

JScrollPane scrollsend = new JScrollPane(textmessagesend);
scrollsend.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

textmessagesend.setWrapStyleWord(true);
textmessagesend.setLineWrap(true);

panelsend.add(scrollsend);
framesend.add(panelsend);
framesend.setVisible(true);

framesend.add(panelsend);
framesend.setVisible(true);
}
});

最佳答案

What can I write instead of this?

布局。对于这个,我将使用带有约束的 GridBagLayout ,该约束将为文本区域提供剩余的可用空间,但尊重文本字段的初始大小(即不展开它)。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class SendFrame2 {

private JComponent ui = null;
JLabel labelsendname = new JLabel("Name: ", JLabel.TRAILING);
JTextField textsendname = new JTextField(20); //suggest a size in columns
JLabel labelmessagesend = new JLabel("Message: ", JLabel.TRAILING);
JTextArea textmessagesend = new JTextArea(15, 45);

SendFrame2() {
initUI();
}

public void initUI() {
if (ui != null) {
return;
}
GridBagConstraints gbc = new GridBagConstraints(
0, 0, 1, 1, 0, 0,
GridBagConstraints.BASELINE_TRAILING,
GridBagConstraints.BOTH,
new Insets(5,5,5,5),
4, 2);

ui = new JPanel(new GridBagLayout());
ui.setBorder(new EmptyBorder(10, 10, 10, 10));

ui.add(labelsendname, gbc);
gbc.gridy = 1;
ui.add(labelmessagesend, gbc);
gbc.gridx = 1;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.VERTICAL;
gbc.anchor = GridBagConstraints.BASELINE_LEADING;
ui.add(textsendname, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
JScrollPane jsp = new JScrollPane(
textmessagesend,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
ui.add(jsp, gbc);
}

public JComponent getUI() {
return ui;
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
SendFrame2 o = new SendFrame2();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - JScrollPane 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36830860/

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