我正在编写此程序,该程序在您单击的任何地方都会创建H-Tree设计,并且我应该实现Jmenubar,以便我可以更改递归图纸的颜色。由于某种原因,无论我做什么,它都不会出现。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial") class Echo extends JFrame implements ActionListener, MouseListener, ChangeListener{
private static final int SLIDER_MIN = 1;
private static final int SLIDER_MAX = 11;
private static final int SLIDER_INIT = 1;
private int x;
private int y;
private int rec = SLIDER_INIT;
private Echo() {
super("H-Tree Drawing Pad");
Container canvas = this.getContentPane();
addMenus();
canvas.add(createSlider(), BorderLayout.SOUTH);
this.setSize(800,800);
this.setVisible(true);
this.setLocation(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addMouseListener(this);
}
private void addMenus() {
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
JMenu menuColor = new JMenu("Color");
menuBar.add(menuColor);
JMenuItem mitToBlack = new JMenuItem("Black");
mitToBlack.addActionListener(new ColorListener());
menuColor.add(mitToBlack);
JMenuItem mitToBlue = new JMenuItem("Blue");
mitToBlue.addActionListener(new ColorListener());
menuColor.add(mitToBlue);
JMenuItem mitToRed = new JMenuItem("Red");
mitToRed.addActionListener(new ColorListener());
menuColor.add(mitToRed);
JMenuItem mitToGreen = new JMenuItem("Green");
mitToGreen.addActionListener(new ColorListener());
menuColor.add(mitToGreen);
}
private JPanel createSlider() {
JPanel slider = new JPanel(new BorderLayout());
JSlider electricSlide = new JSlider(JSlider.HORIZONTAL,SLIDER_MIN, SLIDER_MAX, SLIDER_INIT);
electricSlide.addChangeListener(this);
electricSlide.setMajorTickSpacing(2);
electricSlide.setMinorTickSpacing(1);
electricSlide.setPaintTicks(true);
electricSlide.setPaintLabels(true);
electricSlide.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
slider.add(electricSlide);
return slider;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Echo();
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
x = e.getX();
y = e.getY();
repaint();
}
@Override
public void paint(Graphics g) {
g.setColor(Color.BLACK);
drawOrder(this.getRec(),x,y,95,g);
}
private void drawHTree(int x, int y, int size, Graphics g) {
g.drawLine(x, y, x, y + size);
g.drawLine(x + size, y, x + size, y + size);
g.drawLine(x, y + size / 2, x + size, y + size / 2);
}
private void drawOrder(int initiative, int x, int y, int size, Graphics g) {
this.drawHTree(x, y, size, g);
if(initiative > 1) {
this.drawOrder(initiative - 1, x - size/4, y - size/4, size/2, g);
this.drawOrder(initiative - 1, x+size - size/4, y-size/4, size/2, g);
this.drawOrder(initiative -1, x-size/4, y+size-size/4, size/2, g);
this.drawOrder(initiative - 1, x+size-size/4, y+size-size/4, size/2, g);
}
}
@Override
public void stateChanged(ChangeEvent e) {
JSlider origin = (JSlider)e.getSource();
if(!origin.getValueIsAdjusting()) {
setRec((int)origin.getValue());
}
}
public int getRec() {
return rec ;
}
public void setRec(int rec) {
this.rec = rec;
}
private class ColorListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
您已经覆盖了jframe的油漆方法,并且没有将其称为超级方法,而是打破了绘画链。
,例如,您缺少这一点:
@Override
public void paint(Graphics g) {
super.paint(g); // ****** you're missing this ***
g.setColor(Color.BLACK);
drawOrder(this.getRec(), x, y, 95, g);
}
通过不调用超级绘画方法,您的代码现在正在阻止Jframe的子组件(包括其菜单栏(正确绘画。
这么说,实际上,您绝不应该直接在Jframe中绘画,而是在Jpanel的PaintComponent中,即使在这里您也应该称呼超级绘画方法,这里是super.paintComponent(g);
。