大家好我有幾個關於程序的問題: 首先,我製作了2個JPanel,並將它們添加到帶有BorderLayout中心和East的主窗口。 使用Java繪製程序。 JPanel刷新不好
程序看起來像那樣,但是當我開始繪製一些東西時,我的菜單出現在中心邊框佈局上,不知道是爲什麼。
Additionaly當我minimalize或改變窗口大小的圖形自敗:/。
第三件事,我只能畫折線(「拉馬納」),並再次我不知道如何使它畫一條簡單的直線(「Prosta」)。 Atm Prosta和Łamana一樣。
另外有時它會讓我看到幾個形狀(「Koło」) - 例如圓。
我的代碼:
主要
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class edytor extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public static String jaki, co = null;
public static int jak, jak1;
final static float dash1[] = { 10.0f };
final static BasicStroke dashed = new BasicStroke(1.0f,
BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
public edytor() throws HeadlessException {
super("PAINT");
setLayout(new BorderLayout());
setSize(840, 702);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
jak = 10;
co = "Łamana";
jaki = "Czarny";
JButton zapis = new JButton("Zapis");
zapis.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
JPanel prawyPanel = new JPanel(new GridLayout(5, 0));
JPanel lewyPanel = new rysowanie();
lewyPanel.setVisible(true);
prawyPanel.setVisible(true);
final JComboBox<String> coRysujemy = new JComboBox<String>();
coRysujemy.setEditable(true);
coRysujemy.addItem("Łamana");
coRysujemy.addItem("Kwadrat");
coRysujemy.addItem("Koło");
coRysujemy.addItem("Prosta");
prawyPanel.add(coRysujemy);
coRysujemy.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
co = (String) coRysujemy.getSelectedItem();
}
});
final JComboBox<String> jakiKolor = new JComboBox<String>();
jakiKolor.setEditable(true);
jakiKolor.addItem("Czarny");
jakiKolor.addItem("Niebieski");
jakiKolor.addItem("Zielony");
prawyPanel.add(jakiKolor);
jakiKolor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jaki = (String) jakiKolor.getSelectedItem();
}
});
final JComboBox<String> jakGrube = new JComboBox<String>();
jakGrube.setEditable(true);
jakGrube.addItem("10");
jakGrube.addItem("20");
jakGrube.addItem("30");
prawyPanel.add(jakGrube);
jakGrube.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jak = jakGrube.getSelectedIndex();
}
});
final JComboBox<String> jakRysowane = new JComboBox<String>();
jakRysowane.setEditable(true);
jakRysowane.addItem("Nie Przerywane");
jakRysowane.addItem("Przerywane");
prawyPanel.add(jakRysowane);
prawyPanel.add(zapis);
lewyPanel.setBackground(Color.WHITE);
add(lewyPanel, BorderLayout.CENTER);
add(prawyPanel, BorderLayout.EAST);
jakRysowane.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
jak1 = jakRysowane.getSelectedIndex();
}
});
}
public static void main(String[] args) {
edytor ramka = new edytor();
ramka.setVisible(true);
}
}
繪圖窗口面板
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
public class rysowanie extends JPanel {
/**
*
*/
private static final long serialVersionUID = 636480153152364773L;
private int squareX;
private int squareY;
private int squareW;
private int squareH;
private int xstart, ystart, xend, yend;
public rysowanie() {
// TODO Auto-generated constructor stub
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
if (edytor.jak == 1) {
edytor.jak = 10;
}
if (edytor.jak == 2) {
edytor.jak = 20;
}
if (edytor.jak == 3) {
edytor.jak = 30;
}
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
squareX = e.getX();
squareY = e.getY();
repaint();
}
public void mouseReleased(MouseEvent e) {
if (e.getX() > squareX)
squareW = e.getX() - squareX;
else {
squareW = squareX - e.getX();
squareX = e.getX();
}
if (e.getY() > squareY)
squareH = e.getY() - squareY;
else {
squareH = squareY - e.getY();
squareY = e.getY();
}
repaint();
}
});
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
xstart = e.getX();
ystart = e.getY();
repaint();
}
public void mouseReleased(MouseEvent e) {
xend = e.getX();
yend = e.getY();
repaint();
}
});
if (edytor.co == "Kwadrat") {
if (edytor.jaki == "Niebieski") {
g.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawRect(squareX, squareY, squareW, squareH);
}
if (edytor.co == "Czarny") {
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawRect(squareX, squareY, squareW, squareH);
}
if (edytor.jaki == "Zielony") {
g.setColor(Color.GREEN);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawRect(squareX, squareY, squareW, squareH);
}
}
if (edytor.co == "Łamana") {
if (edytor.jaki == "Niebieski") {
g.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g2.drawLine(xstart, ystart, xend, yend);
}
if (edytor.jaki == "Czarny") {
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawLine(xstart, ystart, xend, yend);
}
if (edytor.jaki == "Zielony") {
g.setColor(Color.GREEN);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawLine(xstart, ystart, xend, yend);
}
}
if (edytor.co == "Koło") {
if (edytor.jaki == "Niebieski") {
g.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawOval(squareX, squareY, squareW, squareH);
}
if (edytor.jaki == "Czarny") {
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawOval(squareX, squareY, squareW, squareH);
}
if (edytor.jaki == "Zielony") {
g2.setColor(Color.GREEN);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g2.drawOval(squareX, squareY, squareW, squareH);
}
}
if (edytor.co == "Prosta") {
if (edytor.jaki == "Niebieski") {
g.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g2.drawLine(xstart, ystart, xend, yend);
}
if (edytor.jaki == "Czarny") {
g.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawLine(xstart, ystart, xend, yend);
}
if (edytor.jaki == "Zielony") {
g.setColor(Color.GREEN);
g2.setStroke(new BasicStroke(edytor.jak));
if (edytor.jak1 == 1) {
g2.setStroke(edytor.dashed);
}
g.drawLine(xstart, ystart, xend, yend);
}
}
}
}
很想任何提示。
[JPanel repaint issue]的可能重複(http://stackoverflow.com/questions/7213178/jpanel-repaint-issue) – trashgod