我的代码中有一点问题。单击udpate按钮后,它应该显示玻璃板。但它没有,我不知道为什么。如果我将setVisibility(true)设置为构造函数中的glasspane,它就可以工作。有人可以帮我吗?这是我的代码
public class ViewClientSettings extends JFrame {
private static final long serialVersionUID = 8498637682155215375L;
private JTextField [] fields;
private JLabel [] label;
private JButton update;
private JComponent glass;
private static final String IPADDRESS_PATTERN =
"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
public ViewClientSettings(String s){
super(s);
this.setLayout(null);
this.setSize(210, 150);
JPanel panel = new JPanel(new GridLayout(2,2,0,15));
panel.setBounds(0, 0, 210, 80);
fields = new JTextField[2];
label = new JLabel[2];
label[0] = new JLabel("server ip");
panel.add(label[0]);
fields[0] = new JTextField(ClientSettings.getServerIP());
panel.add(fields[0]);
label[1] = new JLabel("server port");
panel.add(label[1]);
fields[1] = new JTextField(ClientSettings.getServerPort()+"");
panel.add(fields[1]);
add(panel);
update = new JButton("update");
update.setBounds(50, 85, 100, 25);
update.addActionListener(new Listener());
add(update);
this.setVisible(false);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
this.setLocation(280, 310);
this.setResizable(false);
setGlassPane(glass = new Glass());
lock();
}
public void lock() {
glass.setVisible(true);
}
public void unlock() {
glass.setVisible(false);
}
@SuppressWarnings("serial")
private class Glass extends JComponent{
private String message = "checking";
private ImageIcon icon = new ImageIcon("gif/test.gif");
public Glass() {
this.setBackground(Color.WHITE);
this.setFont(new Font("Default", Font.BOLD, 10));
}
@Override
protected void paintComponent(Graphics g) {
// enables anti-aliasing
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// gets the current clipping area
Rectangle clip = g.getClipBounds();
// sets a 65% translucent composite
AlphaComposite alpha = AlphaComposite.SrcOver.derive(0.65f);
Composite composite = g2.getComposite();
g2.setComposite(alpha);
// fills the background
g2.setColor(getBackground());
g2.fillRect(clip.x, clip.y, clip.width, clip.height);
// draw gif
icon.paintIcon(this,g2,75,25);
// draws the text
g2.setColor(Color.BLACK);
g2.drawString(message, 85, 60);
// draws the content of the progress bar
Paint paint = g2.getPaint();
g2.setPaint(paint);
g2.setComposite(composite);
}
}
private class Listener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
lock();
glass.repaint();
repaint();
if(fields[0].getText().trim().compareTo("") != 0 && fields[1].getText().trim().compareTo("") != 0){
if(fields[0].getText().trim().matches(IPADDRESS_PATTERN) && fields[1].getText().trim().matches("^\\d+$")){
String ip = fields[0].getText().trim();
int port = Integer.valueOf(fields[1].getText());
try {
InetAddress connectionAddress = InetAddress.getByName(ip);
Socket mySocket = new Socket(connectionAddress, port);
System.out.println("connected to address " + ip);
mySocket.close();
}
catch (IOException en) {
System.out.println("unable to connect to host " +ip);
JOptionPane.showMessageDialog(ViewClientSettings.this, "No connection to this server","Warning",JOptionPane.WARNING_MESSAGE);
return;
}
ClientSettings.setServerIP(fields[0].getText().trim());
ClientSettings.setServerPort(Integer.valueOf(fields[1].getText().trim()));
JOptionPane.showMessageDialog(ViewClientSettings.this, "Settings updated","Success",JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
unlock();
}
else
JOptionPane.showMessageDialog(ViewClientSettings.this, "False Settings","Warning",JOptionPane.WARNING_MESSAGE);
}
else
JOptionPane.showMessageDialog(ViewClientSettings.this, "Fill all settings","Warning",JOptionPane.WARNING_MESSAGE);
}
}
}
提前谢谢