2013-12-10 318 views
0

我有以下程序。當我改變列的大小,並點擊任何中心,左側或右側對齊按鈕,如果列的大小太高,那麼一切都會改變。我希望窗戶的尺寸增加,並且面板的佈置沒有任何變化。 有人可以提出一些見解。調整窗口大小

package workingwithjtextfields; 

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


public class WorkingwithJTextFields extends JFrame { 

    private JTextField jtfMessage = new JTextField(100); 
    private JTextField jtfColumn = new JTextField(5); 
    private JRadioButton jrbLeft, jrbCenter, jrbRight; 

    public static void main(String[] args) //Main program begins here. 
    { 
     JFrame frame = new WorkingwithJTextFields();//Instantiating an object. 
     //frame.pack(); 
     frame.setTitle("Exercise 17.11");//Setting the frame title. 
     frame.setSize(500, 110);//Setting the size. 
     frame.setLocationRelativeTo(null);//Setting the location. 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Default closing options. 
     frame.setVisible(true);//Setting visibility to true. 

    }//End of main program 

    public WorkingwithJTextFields() { 

     // jtfMessage.setColumns(100); 
     final JPanel parent = new JPanel(); 
     parent.setLayout(new GridLayout(2, 1, 3, 3)); 

     final JPanel p1 = new JPanel(); 
     p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0)); 

     p1.add(new JLabel("TextField", SwingConstants.CENTER)); 

     jtfMessage = new JTextField("Type anything", SwingConstants.RIGHT); 
     jtfMessage.setHorizontalAlignment(SwingConstants.CENTER); 
     jtfMessage.setColumns(30); 
     p1.add(jtfMessage); 

     parent.add(p1); 

     JPanel jpRadioButtons = new JPanel(); 
     jpRadioButtons.setLayout(new GridLayout(1, 3)); 
     jpRadioButtons.add(jrbLeft = new JRadioButton("Left")); 
     jpRadioButtons.add(jrbCenter = new JRadioButton("Center")); 
     jpRadioButtons.add(jrbRight = new JRadioButton("Right")); 
     jpRadioButtons.setBorder(new TitledBorder("Horizontal Border")); 

     final JPanel p2 = new JPanel(); 
     p2.setLayout(new GridLayout(1, 2, 1, 1)); 

     p2.add(jpRadioButtons); 

     JPanel p3 = new JPanel(); 
     p3.setLayout(new GridLayout(1, 1, 1, 1)); 
     p3.add(new JLabel("Column Size")); 

     jtfColumn = new JTextField("60", SwingConstants.RIGHT); 
     jtfColumn.setHorizontalAlignment(SwingConstants.CENTER); 

     p3.add(jtfColumn); 
     Border lineBorder = new LineBorder(Color.LIGHT_GRAY, 1); 
     p3.setBorder(lineBorder); 
     p2.add(p3); 
     parent.add(p2); 
     add(parent); 

     jrbLeft.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         jtfMessage.setHorizontalAlignment(SwingConstants.LEFT); 
         jrbCenter.setSelected(false); 
         jrbRight.setSelected(false); 
         jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText())); 
         p1.revalidate(); 
         p1.repaint(); 

        } 
       } 
     ); 

     jrbCenter.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         jtfMessage.setHorizontalAlignment(SwingConstants.CENTER); 
         jrbLeft.setSelected(false); 
         jrbRight.setSelected(false); 
         jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText())); 
        } 
       } 
     ); 

     jrbRight.addActionListener(
       new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
         jtfMessage.setHorizontalAlignment(SwingConstants.RIGHT); 
         jrbCenter.setSelected(false); 
         jrbLeft.setSelected(false); 
         jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText())); 
        } 

       } 
     ); 

    } 

} 

回答

0

當您更改字段的列大小,您將需要調用pack領域的窗口,以使其將窗口調整爲它所包含

例如組件的首選大小...

jrbLeft.addActionListener(
     new ActionListener() { 
      public void actionPerformed(ActionEvent e) { 
       jtfMessage.setHorizontalAlignment(SwingConstants.LEFT); 
       jrbCenter.setSelected(false); 
       jrbRight.setSelected(false); 
       jtfMessage.setColumns(Integer.parseInt(jtfColumn.getText())); 
       p1.revalidate(); 
       SwingUtilities.getWindowAncestor(p1).pack(); 
       SwingUtilities.getWindowAncestor(p1).setLocationRelativeTo(null); 
      } 
     } 
); 
+0

即將提到包,但你在我之前做過; Kudos! – 2013-12-10 01:37:09

+0

@Shingetsu這裏希望它有效;)(其實我測試過它,但你總是用這些東西交叉你的手指;)) – MadProgrammer

+0

謝謝你們..它工作得很好。 – user2918968

0

Java應用程序使用佈局管理來控制這一點,如果你想控制它100%,你將不得不使用一個非標準的組件如NetBeans的AbsoluteLayout,但隨後你將不得不包括它的.jar您應用。