-2
私はクリックするとどこでもH-Treeデザインを作成するこのプログラムを書いています。再帰描画の色を変更できるようにJMenuBarを実装することになっています。何らかの理由で、私が何をしてもそれは現れません。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
}
}