我尝试在框架中运行进度条,但它无法正常工作。我试图在我的第二个java类中显示可见,但设置visible(true)将其显示为错误。 希望你们能帮助我解决我的问题/错误
在我的第二个java类中显示错误: “线程中的异常”主“ java.lang.Error:未解决的编译问题:方法 对于类型mgfinancewindow“
,setvisible(boolean)未定义First java class: mgfinancewindow.java
package mgfinance;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
public class mgfinancewindow {
private JFrame frame;
public JProgressBar progressBar;
public JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mgfinancewindow window = new mgfinancewindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mgfinancewindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("MG Finances");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1362, 705);
frame.getContentPane().add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.DARK_GRAY);
panel_1.setBounds(0, 646, 1362, 59);
panel.add(panel_1);
panel_1.setLayout(null);
lblNewLabel = new JLabel("Loading...");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setBounds(1139, 0, 114, 34);
panel_1.add(lblNewLabel);
progressBar = new JProgressBar();
progressBar.setBackground(new Color(0, 51, 51));
progressBar.setBounds(0, 34, 1362, 14);
panel_1.add(progressBar);
JLabel lblMgFinance = new JLabel("MG Finance");
lblMgFinance.setHorizontalAlignment(SwingConstants.CENTER);
lblMgFinance.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblMgFinance.setForeground(Color.BLUE);
lblMgFinance.setBounds(0, 11, 1362, 635);
panel.add(lblMgFinance);
}
}
第二个java类:progressbar.java
package mgfinance;
public class progress {
public static void main(String[] args) throws InterruptedException{
mgfinancewindow load = new mgfinancewindow();
for(int i=0; i<=100; i++){
Thread.sleep(150);
load.setvisible(true);
load.lblNewLabel.setText("Loading..."+ i);
load.progressBar.setValue(i);
}
}
}
答案 0 :(得分:0)
您的mgfinancewindow
课程不是JComponent
来支持setVisible()
方法本身。其中的JFrame
具有setVisible
方法。
在您编写代码的方式中,为了快速解决您的问题,您必须在frame.setVisible(true);
类的initialize()
方法的末尾(最后一条语句)写下mgfinancewindow
并删除{{ 1}}来自load.setvisible(true);
类的main
方法:
progress
和
public class progress {
public static void main(String[] args) throws InterruptedException {
mgfinancewindow load = new mgfinancewindow();
for (int i = 0; i <= 100; i++) {
Thread.sleep(150);
//load.setvisible(true);
load.lblNewLabel.setText("Loading..." + i);
load.progressBar.setValue(i);
}
}
}
<强>但是强>:
当您创建像import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
public class mgfinancewindow {
private JFrame frame;
public JProgressBar progressBar;
public JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mgfinancewindow window = new mgfinancewindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public mgfinancewindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame("MG Finances");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 1362, 705);
frame.getContentPane().add(panel);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBackground(Color.DARK_GRAY);
panel_1.setBounds(0, 646, 1362, 59);
panel.add(panel_1);
panel_1.setLayout(null);
lblNewLabel = new JLabel("Loading...");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setForeground(Color.WHITE);
lblNewLabel.setBounds(1139, 0, 114, 34);
panel_1.add(lblNewLabel);
progressBar = new JProgressBar();
progressBar.setBackground(new Color(0, 51, 51));
progressBar.setBounds(0, 34, 1362, 14);
panel_1.add(progressBar);
JLabel lblMgFinance = new JLabel("MG Finance");
lblMgFinance.setHorizontalAlignment(SwingConstants.CENTER);
lblMgFinance.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
lblMgFinance.setForeground(Color.BLUE);
lblMgFinance.setBounds(0, 11, 1362, 635);
panel.add(lblMgFinance);
frame.setVisible(true);
}
}
这样在内部创建mgfinancewindow
的组件时,大多数时候最好从JFrame
扩展然后您可以从中创建一个对象,并在程序的main方法中调用JFrame
方法。最好不要在该组件中调用setVisible
,因为在另一个类中,有时您想要创建并初始化setVisible
,但您不希望立即显示它。
另一个提示:
根据 Java编码约定:
您可能需要查看其他java编码约定here。