我必须为项目构建一个小型Java小程序。我之前从未使用过applet。因此,我对可用的各种内置功能了解不多。这个小程序的布局如下:
到目前为止,我已成功分割屏幕并为每个部件创建单独的面板,并在其中添加相应的组件。
底部图像不完全可见。并且当每个图像不适合面板时,也不会出现滚动条。
我尝试使用setSize()
,setMinimumSize()
方法,但它不会在输出中产生任何变化。你可以帮我解决上面的问题吗?
/*<applet code=DOSlayout.java width=400 height=400>
</applet>*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DOSlayout extends JApplet implements ActionListener {
Button ViewButton;
Panel1 Top;
Panel2 LeftSide;
Panel3 RightSide;
Label l1,l2,l3;
Image img;
public void init() {
setSize(400,400);
setLayout(new BorderLayout());
Top = new Panel1();
LeftSide = new Panel2();
RightSide = new Panel3();
Top.setSize(getSize().width, getSize().height/3);
LeftSide.setSize(getSize().width,getSize().height/3);
RightSide.setSize(getSize().width,getSize().height/3);
//RightSide.setMinimumSize (new Dimension(400, 10000));
add(Top, BorderLayout.NORTH);
add(LeftSide, BorderLayout.CENTER);
add(RightSide, BorderLayout.SOUTH);
ViewButton = new Button("View");
l1 = new Label("North");
l2 = new Label("East");
l3 = new Label("West");
Top.add(ViewButton);
Top.add(l1);
//LeftSide.add(l2);
//RightSide.add(l3);
ViewButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
class Panel1 extends JPanel {
Panel1() {
super();
}
public void paint(Graphics g) {
}
}
class Panel2 extends JPanel {
Panel2() {
super();
}
private Image img;
public void init()
{
img = null;
}
public void loadImage()
{
try
{
img = getImage(getCodeBase(), "input1.png");
}
catch(Exception e) { }
}
public void paint(Graphics g)
{
if (img == null)
loadImage();
g.drawImage(img, 0, 0, this);
//g.drawImage(img,0,0,(int)getBounds().getWidth(), (int)getBounds().getHeight(),this);
}
}
class Panel3 extends JPanel {
Panel3() {
super();
}
private Image img;
public void init()
{
img = null;
//setSize(400,400);
}
public void loadImage()
{
try
{
img = getImage(getCodeBase(), "input2.png");
}
catch(Exception e) { }
}
public void paint(Graphics g)
{
if (img == null)
loadImage();
g.drawImage(img, 0, 0, this);
//g.drawImage(img,0,0,(int)getBounds().getWidth(), (int)getBounds().getHeight(),this);
}
}}
答案 0 :(得分:4)
从ImageViewer中使用的容器开始。它将图像集中在滚动窗格内,其滚动条可靠地显示。
setSize(..)
。大小在HTML中。