【发布时间】:2013-09-16 02:12:42
【问题描述】:
我正在创建一个 GUI,并且对 swing 和 awt 还很陌生。我正在尝试创建一个 gui,它在启动时将背景设置为图像,然后使用一种方法来创建各种幻灯片。我已经尝试过了,我没有附加到代码上,所以我可以同时接受修订和/或全新的概念。
编辑(2013 年 9 月 15 日):我在播放幻灯片时遇到了问题,我似乎无法让它工作。
这是我当前的代码。
public class MainFrame extends JFrame{
JLabel backgroundL = null;
private JLabel bakckgroundL;
BufferedImage backimg;
Boolean busy;
double width;
double height;
public MainFrame() throws IOException {
initMainframe();
}
public void initMainframe() throws IOException {
//misc setup code, loads a default jpg as background
setTitle("Pemin's Aura");
busy = true;
String backgroundDir = "resources/frame/background.jpg";
backimg = ImageIO.read(new File(backgroundDir));
backgroundL = new JLabel(new ImageIcon(backimg));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
refreshframe();
setVisible(true);
busy = false;
}
public void adjSize() { // the attempted start of a fullscreen mode
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
width = this.getWidth();
height = this.getHeight();
setVisible(true);
}
public void setmastheadText() {//unfinished code
busy = true;
busy = false;
}
public void setbackground() {
add(backgroundL);
}
public void refreshframe() { //should refresh image?
setSize(2049, 2049);
setSize(2048, 2048);
}
public void loadingscreen() throws IOException, InterruptedException {
//this is the code in question that is faulty.
if (busy == false) {
busy = true;
String backgroundDir1 = "resources/frame/background.jpg";
String backgroundDir2 = "resources/frame/scr1.jpg";
String backgroundDir3 = "resources/frame/scr2.jpg";
BufferedImage backimg1 = ImageIO.read(new File(backgroundDir1));
BufferedImage backimg2 = ImageIO.read(new File(backgroundDir2));
BufferedImage backimg3 = ImageIO.read(new File(backgroundDir3));
backgroundL = new JLabel(new ImageIcon(backimg1));
Thread.sleep(2000);
setbackground();
setVisible(true);
backgroundL = new JLabel(new ImageIcon(backimg2));
setbackground();
setVisible(true);
Thread.sleep(2000);
bakckgroundL = new JLabel(new ImageIcon(backimg3));
setbackground();
setVisible(true);
if(backimg != null) {
backgroundL = new JLabel(new ImageIcon(backimg));;
}
}
busy = false;
}//end of loading screen
【问题讨论】:
-
您在哪里有问题?或者你想要代码审查?
-
我会在我的问题中澄清这一点,感谢您提醒我。我想要一个代码审查,但我的问题是我似乎无法让“幻灯片”工作。
-
遵循标准 Java 命名约定。方法名称中的单词应在第一个单词之后大写。 (即,您需要做的就是通过 JDK 中的方法名称来学习)。 refreshFrame() 方法是一段糟糕的代码。没有理由使用两个不同的值两次调用相同的方法。实际上创建框架的常规方法是使用 pack() 方法,并让每个组件以自己的大小显示。
-
改善自己的最好方法是接受真实而直率的建议。 @camickr,谢谢。我意识到 pack() 是最常见的方式,并且我熟悉它的作用。但是,如果我想有空白,只有背景,我不会使用 pack()。之前试过,没用,虽然有JLable,但是框架变成了最小的,所以如果你想帮助我,请这样做,这将是一个很大的帮助
-
the frame became the smallest it could possibly be even though there was a JLable- 您是否阅读过 JLabel API 并点击How to Use Labels上的 Swing 教程链接?它有一个关于如何更好地构建程序的工作示例,它显示带有图像的标签,代码使用 pack() 并且框架大小合适。我猜不出你可能做错了什么。这就是为什么您应该从一个工作示例开始并对其进行修改。
标签: java swing jframe jlabel javax.imageio