Java 如何限制JButton来更改它';s重新验证后的尺寸
我在JFrame上嵌入了一个面板(带有GridLayout(0,1) 根据我的要求:-Java 如何限制JButton来更改它';s重新验证后的尺寸,java,swing,resize,runtime,jbutton,Java,Swing,Resize,Runtime,Jbutton,我在JFrame上嵌入了一个面板(带有GridLayout(0,1) 根据我的要求:- 我在上面声明的面板上成对添加(JButton,JTextArea) 现在,点击任意一对的JButton,它的JTextArea应该被删除,而在重新点击JButton时,它的JTextArea应该被再次添加 除了下面列出的两个问题外,一切正常: JButton的尺寸在重新验证时更改 JButton的初始尺寸非常大(如何限制JButton的尺寸) 作为参考,看看这个 请查看以下修改后的代码列表:- TestF
- 我在上面声明的面板上成对添加(JButton,JTextArea)
- 现在,点击任意一对的JButton,它的JTextArea应该被删除,而在重新点击JButton时,它的JTextArea应该被再次添加
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() ;
}
}
谢谢,
Rits:)您应该使用其他布局。将组件绘制到可用的最大尺寸。例如,使用垂直BoxLayout而不是GridLayout
JPanel
(BorderLayout
)
(默认情况下具有JPanel
)对于FlowLayout
,此JButton
放在JPanel
BorderLayout.NORTH
put-thenJTextArea
BorderLayout.CENTER
JComponents
之间切换或删除然后添加新的JComponent
的情况下hide/visible
特定JPanel
或JComponent
使用方法setVisible()
@rits从这里开始:+1我曾经用你的头像作为我的桌面背景DThnks用于您的输入。我尝试了这种方法,但是setVisible(false)留下了一个空白,这不是我的要求。根据要求,这个空的空间应该被其他的文本空间使用。+ 1还考虑一个<代码> JTooBar < /C>(而不是<代码> jPosie<代码> > <代码> FlowLayout < /代码>中的<代码> JButton < /Cort>约束。@安得烈:JTooBar。Addit(BTN,JToSorBar。North)抛出非法组件位置。jtoolbar.add(btn)工作正常,但在左右位置显示[JButton,JTextArea]。在网上搜索“java垂直BoxLayout示例”和voilla…,效果很好。接受你的答案……:)
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;
}
}