【发布时间】:2015-12-16 14:50:44
【问题描述】:
Okai,除了我命名为项目 2 的第二个私有类之外,基本上一切正常。在这个程序中,构建了一个 2 x 2 的网格,现在在一个网格空间 (Project3) 中,我想要一个图像显示出来。
public class Project1 extends JFrame
{
private Project2 topleft; // Buttons
private Project3 topright; // Picture
private Project4 bottomleft; // Schedule
//private Project5 bottomright; // Help - Pad
// Constructor
public Project1() throws IOException
{
// Display a title.
setTitle(" UAE University Interactive Course Calculator");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a GridLayout manager.
setLayout(new GridLayout(2, 2));
// Create the custom panels.
topleft = new Project2();
topright = new Project3();
bottomleft = new Project4();
//bottomright = new Project5();
//bottomright = new Project5();
// Create the button panel.
add(topleft);
add(topright);
add(bottomleft);
//add(bottomright);
// setting formatting options
pack();
setResizable(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
// Main method
public static void main(String[] args) throws IOException
{
new Project1();
}
}
这是我到目前为止的地方,也许我的逻辑是错误的。我所需要的只是要显示的图像,仅此而已
public class Project3 extends JPanel
{
private JButton run,fancyButton, hel, but, done_by,exit, past;
public Project3()
{
Icon java1 = new ImageIcon( "untitled1.jpg" );
fancyButton = new JButton( "Fancy Button", java1 );
add(fancyButton);
}
}
编辑!!!:okai 现在可以了,但它是一个带按钮的图像,我希望它成为一个没有按钮的图像吗?任何想法?
【问题讨论】:
-
那么你的问题是什么?
-
你永远不会在任何东西上添加
fancyButton -
我只是想知道我哪里错了,只想有人告诉我如何调整我的程序,使它成为一个带有简单图像文件的程序
-
在
Project3中尝试在fancyButton = new JButton( "Fancy Button", java1 );下添加add(fancyButton);。也可以考虑看看Reading/Loading an Image -
参考此链接Java ImageIcon/Icon and JLabel is not working。很可能是创建和加载图像图标的问题。
标签: java