JFrame:如何使用类似行符号的行为制作简单的布局?

时间:2015-11-03 17:03:54

标签: java swing layout demo

我尝试在Java 1.6中使用Swing创建一个包含3个元素的JFrame:一个是页面的开头,第二个是同一行,左边有一些间距,第三个是新行。但经过几次批评后失败了:

BorderLayout.[SOUTH|WEST|EAST|NORTH]
BorderLayout.[LINE_START|LINE_END|AFTER_LINE_END|AFTER_LAST_LINE]
FlowLayout.LEFT
BoxLayout.[X_AXIS|Y_AXIS]

它显示右边的唯一方法是使用:

container.add(firstElement, BorderLayout.WEST);
container.add(secondElement, BorderLayout.EAST);
container.add(thirdElement, BorderLayout.SOUTH); 

以下是我的程序的当前版本:

import javax.swing.*;
import java.awt.*;
//import java.awt.event.*;
//import java.lang.reflect.*;

public class MainClass {
    public static void main(String[] args) {

        JFrame frame = new JFrame("JFrame Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container container = frame.getContentPane();

        JTextField field = new JTextField();
        JButton button = new JButton("Enter");
        JTextArea outputarea = new JTextArea("Outputtext before input.");
        outputarea.setEditable(false);

        container.add(field, BoxLayout.X_AXIS);
        container.add(button, BoxLayout.X_AXIS);
        container.add(outputarea, BoxLayout.Y_AXIS);

        field.setPreferredSize(new Dimension(600, 30));
        button.setPreferredSize(new Dimension(100, 30));
        //button.setMargin(new Insets(0, 10, 0, 0)); //left-margin
        outputarea.setPreferredSize(new Dimension(700, 300));

        container.setPreferredSize(new Dimension(800, 400));

        frame.pack();
        frame.setVisible(true);
    }
}

虽然我想要达到这样的目的:
https://dl.dropboxusercontent.com/u/72032667/JFrame_Demo.html
来源:

<!doctype html>
<html lang="en">
<head>
<title>JFrame Demo</title>
<style type="text/css">
  body {width:800px; min-height:400px;}
  * {float:left; margin:0; color:inherit;}
  #input {width:600px;}
  #button {margin-left:10px; background:#AAA; padding:2px 5px; border:1px solid black;}
  #textarea {width:700px; height:300px; resize:none; white-space:nowrap;}
</style>
<script type="text/javascript">
  function myAction() {
    var inputElement = document.getElementById('input');
    var outputElement = document.getElementById('textarea'); 
    outputElement.innerHTML = myMethod(inputElement.value);

    function myMethod(str) {
      // Do something more useful than this:
      return "You wrote: "+str;
    }
  }
</script>
</head>
<body>
  <input id="input" />
  <a href="#" onclick="myAction()"><p id="button">Enter</p></a>
  <br>
  <textarea id="textarea" readonly spellcheck="false" warp="off"></textarea>
</body>
</html>

我看到一些例子,他们给JFrame的构造函数一个Component作为选择布局的参数,但是还没有理解它是如何工作的。
编辑:我不接受这3个元素的嵌套解决方案。

1 个答案:

答案 0 :(得分:3)

好的,我知道这不是你想要的答案,抱歉。但我只是想告诉你我的意见以及如何做到这一点。这不是一个直接的答案,但也许你会在看到它后改变主意:P(特别是关于setPreferredSize(),因为这不是真的建议这样做)。这几乎适用于任何帧大小:

代码:

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainClass {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainClass();
            }
        });
    }

    public MainClass() {

        JFrame frame = new JFrame("JFrame Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea outputarea = new JTextArea();
        outputarea.setBorder(new LineBorder(Color.GRAY));
        outputarea.setWrapStyleWord(true);
        outputarea.setLineWrap(true);
        outputarea.setEditable(false);

        JTextField field = new JTextField();
        field.setBorder(new CompoundBorder(new EmptyBorder(0, 0, 3, 0), field.getBorder()));

        JButton button = new JButton("Enter");
        button.setBackground(Color.LIGHT_GRAY);
        button.setFocusPainted(false);
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                outputarea.setText("You wrote: " + field.getText());
            }
        });

        // This panel is just needed in order to set a border for the button
        // without losing it's margin.
        BorderPanel buttonPanel = new BorderPanel();
        buttonPanel.setBorder(new CompoundBorder(new EmptyBorder(0, 10, 0, 30), new LineBorder(Color.BLACK)));
        buttonPanel.add(button);

        BorderPanel northPanel = new BorderPanel();
        northPanel.add(field);
        northPanel.add(buttonPanel, BorderLayout.EAST);

        BorderPanel mainPanel = new BorderPanel();
        mainPanel.add(outputarea);
        mainPanel.add(northPanel, BorderLayout.NORTH);

        frame.setContentPane(mainPanel);
        frame.setSize(600, 300);
        frame.setVisible(true);

    }

    public class BorderPanel extends JPanel {

        public BorderPanel() {
            setLayout(new BorderLayout());
            setBackground(Color.WHITE);
        }

    }

}

最终结果:

enter image description here