JScrollPane不可见

时间:2016-04-25 01:36:35

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

此处不显示滚动窗格。我知道问题是public void sendMessage() throws IOException{ try{ // get the message to send from somewhere String msgToSend = "foo"; // just dumb text; replace with actual writer.write(msgToSend); writer.write("\n"); // often needed writer.flush(); // flush the buffer } } 。我能写什么而不是这个?

public void buttonAbout1(View v) {
Button buttonAbout1 = (Button) findViewById(R.id.buttonAbout1);
assert buttonAbout1 != null;
    buttonAbout1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(GMOEd.this,About2.class));
        }
    });
}

1 个答案:

答案 0 :(得分:1)

  

我可以写什么而不是这个?

布局。对于这个,我使用带有约束的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);
    }
}