为什么每次我从 JButton 调用它时我的框架都是不可见的?
我正在为我们的小组项目创建一个加载页面,但是每次我调用加载页面类时,它都是不可见的。
这是我的主类:
public static void main(String[] args) {
new caller();
}
这是一个按钮,当您单击该按钮时,您将进入加载页面类。
JFrame frame = new JFrame();
JButton btn = new JButton("Press me!");
caller(){
btn.setBounds(20, 30, 120, 50);
btn.setFocusable(false);
btn.addActionListener(this);
frame.setSize(420, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(null);
frame.add(btn);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn) {
frame.dispose();
new loading_page();
}
}
这是加载页面类,
JLabel lablab = new JLabel();
JProgressBar bar = new JProgressBar();
JFrame frame = new JFrame();
loading_page(){
lablab.setBackground(Color.WHITE);
lablab.setHorizontalAlignment(SwingConstants.CENTER);
lablab.setIcon(new ImageIcon("C:\\Users\\KHAdmin\\Documents\\Pictures\\giphy.gif"));
lablab.setBounds(65, 40, 367, 215);
bar.setForeground(Color.cyan);
bar.setBounds(10, 300, 480, 17);
bar.setValue(0);
bar.setStringPainted(true);
bar.setFont(new Font("MV Boli", Font.BOLD, 15));
bar.setForeground(Color.CYAN);
bar.setBackground(Color.BLACK);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 200, 500, 335);
frame.setVisible(true);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.getContentPane().add(lablab);
frame.getContentPane().add(bar);
int counter =0;
while(counter<=100) {
bar.setValue(counter);
try {
Thread.sleep(50);
if(counter==100) {
frame.dispose();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
counter +=1;
}
{
new caller();
}
}
我认为它位于 while(counter<==100) 语句中。 我使用frame dispose来处理前一个类中的框架,但是当系统调用第三个类(即加载页面类)时,它是不可见的。
I'm creating a Loading Page for our group project, but every time I call the loading page class, it is invisible.
Here is my Main class:
public static void main(String[] args) {
new caller();
}
Here is the button that when you click you will go into the loading page class.
JFrame frame = new JFrame();
JButton btn = new JButton("Press me!");
caller(){
btn.setBounds(20, 30, 120, 50);
btn.setFocusable(false);
btn.addActionListener(this);
frame.setSize(420, 420);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLayout(null);
frame.add(btn);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btn) {
frame.dispose();
new loading_page();
}
}
And here is the loading page class
JLabel lablab = new JLabel();
JProgressBar bar = new JProgressBar();
JFrame frame = new JFrame();
loading_page(){
lablab.setBackground(Color.WHITE);
lablab.setHorizontalAlignment(SwingConstants.CENTER);
lablab.setIcon(new ImageIcon("C:\\Users\\KHAdmin\\Documents\\Pictures\\giphy.gif"));
lablab.setBounds(65, 40, 367, 215);
bar.setForeground(Color.cyan);
bar.setBounds(10, 300, 480, 17);
bar.setValue(0);
bar.setStringPainted(true);
bar.setFont(new Font("MV Boli", Font.BOLD, 15));
bar.setForeground(Color.CYAN);
bar.setBackground(Color.BLACK);
frame.setUndecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400, 200, 500, 335);
frame.setVisible(true);
frame.setLayout(null);
frame.getContentPane().setBackground(Color.BLACK);
frame.getContentPane().add(lablab);
frame.getContentPane().add(bar);
int counter =0;
while(counter<=100) {
bar.setValue(counter);
try {
Thread.sleep(50);
if(counter==100) {
frame.dispose();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
counter +=1;
}
{
new caller();
}
}
I think it's in the while(counter<==100) statement.
I used the frame dispose to dispose the frame from the previous class, but when the system call the 3rd class which is the loading page class, it's invisible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论