我是Swing中的新成员。我试图创建一个800x600的窗口,将其分成两个相等的部分(800x300):第一个窗口是不可变的文本区域,第二个窗口是按钮。JButton忽略指示大小
问题:按钮应该占据窗口的下半部分。但似乎按钮尺寸不正确。
代码:
import javax.swing.*;
import javax.xml.parsers.ParserConfigurationException;
import java.awt.*;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
JFrame.setDefaultLookAndFeelDecorated(true);
final JFrame frame = new JFrame("Lingvo frame");
frame.setPreferredSize(new Dimension(800, 600));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridLayout(4,4,4,4));
final JButton showResultButton = new JButton();
showResultButton.setPreferredSize(new Dimension(800, 300));
showResultButton.setHorizontalAlignment(SwingConstants.CENTER);
showResultButton.setVerticalAlignment(SwingConstants.BOTTOM);
frame.add(panel);
panel.add(showResultButton);
frame.pack();
frame.setVisible(true);
showResultButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
showResultButton.setText("translation");
}
});
}
}
感谢它适用于我 –
你能对齐你的alignt JTextArea文本到中心吗?并设置一些漂亮的字体呢? –
你是什么意思对齐中心?另请参阅[**如何使用字体**](http://docs.oracle.com/javase/tutorial/2d/text/fonts.html)。一旦你知道了,只需使用'textArea.setFont(font)' –