I'm writing the UI for the pet project I've been doing and I'm experimenting with java Graphics, drawing lines, shapes, and stuff. And, I've been trying all day to insert a simple line in a Jpanel but still haven't figured out what went wrong.
package thuake;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.Paint;
import java.awt.Polygon;
import java.awt.geom.Line2D;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
public class Main extends JFrame{
static Dimension DEFAULT_SIZE = new Dimension(530, 320);
static JFrame Frame1 = new JFrame();
static JScrollPane spanel = new JScrollPane();
static JPanel Panel1 = new JPanel();
static MenuBar menu = new MenuBar();
static Menu menusub1 = new Menu("Open");
public static void main(String[] args)
{
start();
}
public static void start (){
Frame1.setLayout(new FlowLayout(FlowLayout.CENTER,5,10));
spanel.add(new draw());
Frame1.add(spanel);
spanel.setBorder(BorderFactory.createLineBorder(Color.black));
spanel.setPreferredSize(new Dimension(500, 500));
Frame1.add(new JButton("ad"));
Frame1.add(new JButton("ad"));
Frame1.add(new JButton("ad"));
Frame1.add(new draw());
Frame1.setMenuBar(menu);
menu.add(menusub1);
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame1.pack();
spanel.setVisible(true);
Frame1.setVisible(true);
System.out.println();
}
static class draw extends Component {
public void paint(Graphics g) {
Graphics2D line = (Graphics2D)g;
line.drawLine(0, 0, 120, 120);
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…