2012-01-18 22 views
3

我有一個嵌入在JFrame上的面板(GridLayout(0,1))。如何限制JButton作爲重新驗證的結果來更改尺寸

按我的要求: -

  • 我在上面定義面板上對加(JButton的,JTextArea中)。
  • 現在,點擊任何一對JButton,它的JTextArea應該被刪除,並且在重新點擊JButton時,應該再次添加JTextArea。

一切工作正常,但低於兩家上市問題:

的JButton的
  1. 尺寸都在重新驗證改變。
  2. 的JButton的初始尺寸非常大(如何限制的JButton的尺寸)

作爲參考,看看這個screenshot

並請在下面找到修改的代碼清單: -

TestFrame.java

package com.test; 

import java.awt.BorderLayout; 
import java.awt.Component; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 

public class TestFrame extends JFrame implements ActionListener{ 

    final JPanel panel ; 

    private TestFrame() { 
     setExtendedState(JFrame.MAXIMIZED_BOTH) ; 
     setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE) ; 
     panel = new JPanel() ; 
     panel.setLayout(new GridLayout(0, 1)) ; 

     for(int i = 1 ; i <= 3 ; ++i){ 
      TButton btn = new TButton("User " + i + " [Toggle Button for TextArea " + i + "] (Click)") ; 
      panel.add(btn) ;  btn.addActionListener(this) ; panel.add(new JScrollPane(btn.getLoggingArea())) ; 
     } 

     add(panel, BorderLayout.CENTER) ; 
     setVisible(true) ; 
     setExtendedState(JFrame.MAXIMIZED_BOTH) ; 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     TButton btn = (TButton) e.getSource() ; 
     JPanel parent = (JPanel) btn.getParent() ; 

     int index = getIndex(parent, btn) ; 

     if(btn.isLoggingAreaVisible()){ 
      parent.remove(parent.getComponent(index + 1)) ; 
      btn.setLoggingAreaVisible(false) ; 
     }else{ 
      parent.add(new JScrollPane(btn.getLoggingArea()), index + 1) ; 
      btn.setLoggingAreaVisible(true) ; 
     } 

     parent.revalidate() ; 
     parent.repaint() ; 
    } 

    private int getIndex(JComponent parent, Component btn) { 

     Component []comps = parent.getComponents() ; 

     for(int i = 0 ; i < comps.length ; ++i){ 
      if(comps[i].equals(btn)){ 
       return i ; 
      } 
     } 

     return -1 ; 
    } 

    public static void main(String[] args) { 
     new TestFrame() ; 
    } 
} 

TButton.java

package com.test; 

import javax.swing.JButton; 
import javax.swing.JTextArea; 

public class TButton extends JButton{ 

    private JTextArea loggingArea ; 
    private boolean loggingAreaVisible = true ; 

    public TButton(String threadName) { 
     super(threadName) ; 
     initComponents(threadName) ; 
    } 

    public void initComponents(String threadName) { 

     String str = "1. By default large buttons are displayed." + "\n" + 
        " But, I want buttons with a little small dimensions." + "\n\n" + 
        "2. On click of above button, above button expands to cover space used by this textArea." + "\n" + 
        " But, what I want, that button size does not changes onClick, only textArea length increases/decreases." ; 
     loggingArea = new JTextArea(getText() + " textArea." + "\n\n" + str + "\n\n" + "Type Here...") ; 
    } 


    public boolean isLoggingAreaVisible() { 
     return loggingAreaVisible; 
    } 

    public void setLoggingAreaVisible(boolean loggingAreaVisible) { 
     this.loggingAreaVisible = loggingAreaVisible; 
    } 

    public JTextArea getLoggingArea() { 
     return loggingArea; 
    } 
} 

謝謝, RITS :)

+0

+1我曾經用你的頭像作爲我的桌面背景的情況下。 :D – mre 2012-01-18 12:45:58

回答

2

使用例如垂直BoxLayout的,而不是網格佈局

+0

在網上搜索「java垂直BoxLayout例如「,和voilla ...,它的工作就像魅力。接受你的答案... :) – mogli 2012-01-18 18:31:32

1

您應該使用另一種佈局。 GridLayout將組件繪製爲可用的最大尺寸。

+0

任何建議,或代碼與例子列表... – mogli 2012-01-18 12:41:38

+1

@rits從這裏開始:http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html – 2012-01-18 12:44:06

3
  1. 返回JPanelBorderLayout)嵌套
    • JPanel(在默認情況下FlowLayout)爲JButton,這JPanel付諸BorderLayout.NORTH
    • JTextArea投料,然後BorderLayout.CENTER
  2. 那裏我看不到revalidate & repaint的原因,o NLY在你JComponents之間切換或刪除,然後添加新JComponent(s)
  3. 對於hide/visible特別JPanelJComponent使用方法setVisible()
+0

爲你的投入所需。我嘗試了這種方法,但setVisible(false)留下了一個空白空間,這不是我的要求。根據要求,這個空白空間應該被其他文本使用。 – mogli 2012-01-18 12:53:16

+0

+1在'NORTH'約束中,考慮'JToolBar'(而不是帶'FlowLayout'的'JPanel')。 – 2012-01-18 12:58:28

+0

@Andrew:jtoolbar.add(btn,JToolBar.NORTH)拋出非法的組件位置。 jtoolbar.add(btn)正常工作,但在左右位置顯示[JButton,JTextArea]。 – mogli 2012-01-18 14:34:25