java - JScrollPane 不可见

标签 java swing jscrollpane layout-manager null-layout-manager

滚动 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/

相关文章:

java - Spring Boot缓存中的咖啡因缓存: Get all cached keys

java - 在选项卡切换上刷新 JPanel 内容

Java GUI - JTextArea 扩展但不收缩

java - 如何检查用户是否在微调器中选择了一个选项

java - 在Java程序中使用结果集

java - JToolBar 防止最小化

java - 将 Object 类型转换为 JButton 类型?

java - JPanel 中行的动态宽度和固定高度

java JScrollPane问题

java - 字段 vs 字段和新对象