我正在尝试绘制一个位于applet中的面板。我有5个面板,并为背景着色,以便我可以看到它们的位置。我试图绘制基本椭圆作为测试,但结果是面板似乎缩小,没有出现椭圆。
我会很乐意帮助你!
非常感谢
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class gui extends Applet{
private JFrame frame = new JFrame("Algorithm Animation");
private JPanel pnlRight = new JPanel();
private JPanel pnlLeft = new JPanel();
private JPanel pnlCenter = new JPanel();
private JPanel pnlTop = new JPanel();
private JPanel pnlBottom = new JPanel();
private JMenuBar mb = new JMenuBar();
private JMenu menuFile = new JMenu("File");
private JMenuItem menuItemQuit = new JMenuItem("Quit");
public void setMenuBar(){
frame.setJMenuBar(mb);
menuFile.add(menuItemQuit);
mb.add(menuFile);
//listener for quit button
menuItemQuit.addActionListener(new ListenMenuQuit());
}
public class ListenMenuQuit implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
}
public void setPanelTop() {
pnlTop.setBackground(Color.BLUE);
}
public void setPanelBottom() {
pnlBottom.setBackground(Color.BLUE);
}
public void setPanelLeft() {
pnlLeft.setBackground(Color.GREEN);
}
public void setPanelCenter() {
pnlCenter.setBackground(Color.RED);
}
public void setPanelRight() {
DrawGraph drawG = new DrawGraph();
pnlRight.add(drawG);
}
public gui() {
setMenuBar();
setPanelTop();
setPanelBottom();
setPanelLeft();
setPanelCenter();
setPanelRight();
//put panels on the frame
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pnlTop, BorderLayout.NORTH);
frame.getContentPane().add(pnlBottom, BorderLayout.SOUTH);
frame.getContentPane().add(pnlLeft, BorderLayout.WEST);
frame.getContentPane().add(pnlCenter, BorderLayout.CENTER);
frame.getContentPane().add(pnlRight, BorderLayout.EAST);
}
private void launch(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main (String[] args) {
JFrame.setDefaultLookAndFeelDecorated(false);
gui g = new gui();
g.launch();
}
}
我还有一个DrawGraph类(我认为它不起作用):
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JPanel;
public class DrawGraph extends JPanel {
Color c1 = Color.BLUE;
Color c2 = Color.RED;
Color c3 = Color.GREEN;
Color c4 = Color.YELLOW;
Color cl1, cl2, cl3, cl4 = Color.BLACK;
int x = 50, y = 100, w = 20, h = 20;
public DrawGraph(){
super();
this.setBackground(Color.ORANGE);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setColor(cl1);
g2.drawLine(x+10, y+10, x+210, y+10);
g2.setColor(cl2);
g2.drawLine(x+210, y+10, x+210, y+210);
g2.setColor(cl3);
g2.drawLine(x+10, y+210, x+210, y+210);
g2.setColor(cl4);
g2.drawLine(x+10, y+10, x+10, y+210);
Ellipse2D e1 = new Ellipse2D.Double(x, y, w, h);
g2.setPaint(c1);
g2.fill(e1);
Ellipse2D e2 = new Ellipse2D.Double(x+200, y, w, h);
g2.setPaint(c2);
g2.fill(e2);
Ellipse2D e3 = new Ellipse2D.Double(x, y+200, w, h);
g2.setPaint(c3);
g2.fill(e3);
Ellipse2D e4 = new Ellipse2D.Double(x+200, y+200, w, h);
g2.setPaint(c4);
g2.fill(e4);
}
}
非常感谢任何帮助。
约什
答案 0 :(得分:2)
您可以使用setPreferredSize()
方法设置DrawGraph面板的宽度和高度。
public DrawGraph(){
super();
this.setBackground(Color.ORANGE);
setPreferredSize(new Dimension(400,400));
}
答案 1 :(得分:0)
您是否尝试过重新包装面板?或者再加载它们?我很久以前在Java工作,但我记得我必须刷新面板才能将实际数据显示在其中。
答案 2 :(得分:0)
从你的:
public class DrawGraph extends JPanel
您应该将此JPanel子类直接放到JFrame中。
frame.getContentPane().add(new DrawGraph(), BorderLayout.EAST);
绝对:
public void setPanelRight() {
DrawGraph drawG = new DrawGraph();
pnlRight.add(drawG);
}
但这只是第一个想法,可能无效。