如何限制 JButton 在重新验证时更改其尺寸
我在 JFrame 上嵌入了一个面板(带有 GridLayout(0,1))。
根据我的要求:-
- 我在上面声明的面板上成对添加(JButton,JTextArea)。
- 现在,单击任何对中的 JButton 时,应删除它的 JTextArea,而重新单击 JButton 时,应再次添加它的 JTextArea。
一切工作正常,除了下面列出的两个问题:
- JButton 的尺寸在重新验证时发生了变化。
- JButton 的初始尺寸非常大(如何限制 JButton 的尺寸)
作为参考,请查看此 。
请找到下面修改后的代码清单:-
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;
}
}
谢谢, 里茨:)
I have a panel (with GridLayout(0,1)) embedded on JFrame.
As per my requirement :-
- I am adding (JButton,JTextArea) in pairs on above declared panel.
- Now, on click of JButton of any pair, it's JTextArea should be removed, and on reclicking JButton, it's JTextArea should be added again.
Everything is working fine, except below two listed problems :
- Dimensions of JButton are changed on revalidation.
- Initial dimensions of JButton are very large(How to restrict dimension of JButton)
For reference, have a look at this .
And Please find below modified Code listings :-
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;
}
}
Thanks,
Rits :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JPanel
(BorderLayout
)JPanel
(默认情况下有FlowLayout
)对于JButton
,此JPanel
放入BorderLayout .北
JTextArea
放入BorderLayout.CENTER
revalidate & 的原因。重新绘制
,仅在您在JComponents
之间切换或删除然后添加新的JComponent
的情况下,隐藏/可见
> 特定的JPanel
或JComponent
使用方法setVisible()
JPanel
(BorderLayout
) nestedJPanel
(has by defaultFlowLayout
) forJButton
, thisJPanel
put to theBorderLayout.NORTH
JTextArea
put to thenBorderLayout.CENTER
revalidate & repaint
, only in the case that you switch betweenJComponents
or remove and then add newJComponent(s)
,hide/visible
particularJPanel
orJComponent
use methodsetVisible()
使用例如垂直 BoxLayout 而不是 GridLayout
Use e.g. vertical BoxLayout instead of GridLayout
您应该使用其他布局。
GridLayout
绘制组件达到最大可用尺寸。You should use another layout.
GridLayout
draws the components to maximum size available.