2014-02-11 50 views
1

我是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"); 
      } 
     }); 
    } 
} 

回答

3

只是你的框架设置为GridLayout。另请注意,只需使用此构造函数JtextArea(rows, columns)pack()即可设置您的框架的尺寸JTextArea。无需将尺寸设置为任何东西。根据JTextArea

的大小,GridLayout将为您处理JButton的大小。此外,还应该从事件派遣线程运行Swing应用程序。见Initial Threads

另请参阅Laying out Components within a Container以了解有关布局管理器的更多信息。

这里的一个实施例运行

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

public class Main { 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       JFrame.setDefaultLookAndFeelDecorated(true); 
       final JFrame frame = new JFrame("Lingvo frame"); 

       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

       final JButton showResultButton = new JButton(); 
       final JTextArea textArea = new JTextArea(20, 60); 

       frame.setLayout(new GridLayout(2, 1)); 
       frame.add(textArea); 
       frame.add(showResultButton); 

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

       showResultButton.addActionListener(new ActionListener() { 

        public void actionPerformed(ActionEvent arg0) { 
         showResultButton.setText("translation"); 
        } 
       }); 
      } 
     }); 
    } 
} 
+0

感谢它适用于我 –

+0

你能对齐你的alignt JTextArea文本到中心吗?并设置一些漂亮的字体呢? –

+0

你是什么意思对齐中心?另请参阅[**如何使用字体**](http://docs.oracle.com/javase/tutorial/2d/text/fonts.html)。一旦你知道了,只需使用'textArea.setFont(font)' –

1

其中按钮被插入具有GridLayout(4,4,4,4)面板。这意味着面板将包含4 * 4个组件,并且所有组件都具有相同的尺寸(!)。

您的代码此刻不包含文本区域。但根据描述,你的布局很可能与

panel.setLayout(new GridLayout(2,1)); 
panel.add(theTextArea); // The top half 
panel.add(button); // The bottom half 

你必须阅读一些关于布局管理器(http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html),以熟悉他们实现。特别是关于他们如何考虑“首选大小”的问题。