最近碰到一个棘手的问题写了一个swing程序,用到JCrollPane组建,初始是调用其setVisible(false),在程序的后端会在调用其setVisible(true),但是不能显示出来,不知道是何缘故!
下面是程序代码:
public class AlertFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private static int count = 1;
private JTextArea detailTxa;
private JScrollPane jScrollPane1;
private JLabel errolLbl;
private JButton cancelBtn;
private JButton detailBtn;
private String simpleMsg = "";
private String detailMsg = "";
public AlertFrame(){
super();
}
public AlertFrame(String simpleMsg,String detailMsg){
super("系统消息");
this.simpleMsg = simpleMsg;
this.detailMsg = detailMsg;
init();
}
public void init(){
WindowUtilities.setNativeLookAndFeel();
setSize(480,260);
setResizable(true);
Box box = new Box(BoxLayout.Y_AXIS);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
JPanel centerPanel = new JPanel(new BorderLayout());
Box centerBox = new Box(BoxLayout.X_AXIS);
centerBox.add(new JLabel(" "));
errolLbl = new JLabel(simpleMsg);
centerBox.add(errolLbl);
centerBox.add(new JLabel(" "));
JLabel titleLbl = new JLabel(" 系统消息 ");
centerPanel.add(centerBox, BorderLayout.CENTER);
centerPanel.add(titleLbl, BorderLayout.NORTH);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
cancelBtn = new JButton(" 取消 ");
cancelBtn.addActionListener(this);
detailBtn = new JButton("详细信息>>");
detailBtn.addActionListener(this);
bottomPanel.add(cancelBtn);
bottomPanel.add(detailBtn);
centerPanel.add(bottomPanel , BorderLayout.SOUTH);
detailTxa = new JTextArea(10,25);
detailTxa.setText(detailMsg);
detailTxa.setVisible(false);
jScrollPane1 = new JScrollPane(detailTxa);
jScrollPane1.setVisible(false);
box.add(centerPanel);
box.add(jScrollPane1);
this.add(box);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == cancelBtn){
this.dispose();
}else{
if((count%2) == 0){
this.jScrollPane1.setVisible(false);
count++;
}
else{
this.jScrollPane1.setVisible(true);
count++;
}
}
}
}
下面是程序代码:
public class AlertFrame extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private static int count = 1;
private JTextArea detailTxa;
private JScrollPane jScrollPane1;
private JLabel errolLbl;
private JButton cancelBtn;
private JButton detailBtn;
private String simpleMsg = "";
private String detailMsg = "";
public AlertFrame(){
super();
}
public AlertFrame(String simpleMsg,String detailMsg){
super("系统消息");
this.simpleMsg = simpleMsg;
this.detailMsg = detailMsg;
init();
}
public void init(){
WindowUtilities.setNativeLookAndFeel();
setSize(480,260);
setResizable(true);
Box box = new Box(BoxLayout.Y_AXIS);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setLayout(new BorderLayout());
JPanel centerPanel = new JPanel(new BorderLayout());
Box centerBox = new Box(BoxLayout.X_AXIS);
centerBox.add(new JLabel(" "));
errolLbl = new JLabel(simpleMsg);
centerBox.add(errolLbl);
centerBox.add(new JLabel(" "));
JLabel titleLbl = new JLabel(" 系统消息 ");
centerPanel.add(centerBox, BorderLayout.CENTER);
centerPanel.add(titleLbl, BorderLayout.NORTH);
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
cancelBtn = new JButton(" 取消 ");
cancelBtn.addActionListener(this);
detailBtn = new JButton("详细信息>>");
detailBtn.addActionListener(this);
bottomPanel.add(cancelBtn);
bottomPanel.add(detailBtn);
centerPanel.add(bottomPanel , BorderLayout.SOUTH);
detailTxa = new JTextArea(10,25);
detailTxa.setText(detailMsg);
detailTxa.setVisible(false);
jScrollPane1 = new JScrollPane(detailTxa);
jScrollPane1.setVisible(false);
box.add(centerPanel);
box.add(jScrollPane1);
this.add(box);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == cancelBtn){
this.dispose();
}else{
if((count%2) == 0){
this.jScrollPane1.setVisible(false);
count++;
}
else{
this.jScrollPane1.setVisible(true);
count++;
}
}
}
}