62,628
社区成员
发帖
与我相关
我的任务
分享
1.rightPanel中加入CardLayout panelLayout;
CardLayout panelLayout = new CardLayout();
rightPanel.setLayout(panelLayout);
2.加入 Panel 到 rightPanel
rightPanel.add(one, "1");
rightPanel.add(two, "2");
rightPanel.add(three, "3");
3.要显示相应 Panel ,你得有监听吧?假设有三个按钮名为b1,b2,b3,按钮的Text分别为1,2,3
public void actionPerformed(ActionEvent e){
String str = ((JButton)e.getSource()).getText();
centerRightPanelLayout.show(centerRightPanel, str); //rightPanel转到相应Panel
int order = Integer.parseInt(str);
centerRightPanel.getComponent(order-1); //取到对应的Panel
}
4.取到对应Panel了,再对其中的JScrollpane判断就行了。
if(scrollBar_V.isVisible()){
upButton.setVisible(true);
downButton.setVisible(true);
}else{
upButton.setVisible(false);
downButton.setVisible(false);
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class tt extends JFrame
implements ActionListener
{
private static final long serialVersionUID = 2656136464519181862L;
public JTextArea display;
public JTextArea chat;
private JButton pass;
public JScrollPane displayArea;
public JScrollPane chatArea;
JPanel pane;
public tt(){
this.setTitle("服务器");
int screenWidth = (int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = (int)java.awt.Toolkit.getDefaultToolkit().getScreenSize().height;
display=new JTextArea();
chat=new JTextArea();
displayArea=new JScrollPane(display);
chatArea=new JScrollPane(chat);
pass=new JButton("发送");
pane=new JPanel();
pane.setLayout(null);
pane.add(displayArea);
pane.add(chatArea);
pane.add(pass);
displayArea.setBounds(0,5,240,100);
chatArea.setBounds(5,125,140,50);
pass.setBounds(160,125,70,50);
this.getContentPane().add(pane).setBackground(Color.white);
this.getContentPane().add(pane);
pass.addActionListener(this);
this.setResizable(false);
this.setSize(240,240);
this.setLocation((screenWidth-300), (screenHeight-280));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
System.out.println(displayArea.getSize());
System.out.println(display.getSize());
}
public static void main(String args[]){
new tt();
}
}
display=new JTextArea();
displayArea=new JScrollPane(display);java.awt.Dimension[width=240,height=100]
java.awt.Dimension[width=237,height=97]java.awt.Dimension[width=240,height=100]
java.awt.Dimension[width=253,height=82]java.awt.Dimension[width=240,height=100]
java.awt.Dimension[width=222,height=126]