我在JFrame上嵌入了一个面板(GridLayout( 0,1 ))。
根据我的要求: -
除了下面列出的两个问题之外,一切都运转良好:
供参考,请查看此。
请查看以下修改后的代码清单: -
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 :(得分:3)
JPanel
(BorderLayout
)嵌套
JPanel
(默认情况下为FlowLayout
)JButton
,此JPanel
投放到BorderLayout.NORTH
JTextArea
放到BorderLayout.CENTER
revalidate & repaint
的原因,只有在JComponents
之间切换或删除然后添加新的JComponent(s)
,hide/visible
特定JPanel
或JComponent
使用方法setVisible()
答案 1 :(得分:2)
使用例如垂直BoxLayout而不是GridLayout
答案 2 :(得分:1)
您应该使用其他布局。 GridLayout
将组件绘制为可用的最大大小。