0

キャンバスを保持する他の 2 つの JPanel の真ん中に buttonPanel が必要なプロジェクトに取り組んでいます (キャンバスは長方形を描画します)。私はこれを行う方法にこだわっています。

私のGUIクラス:

import javax.swing.*; 
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class GUI extends JFrame implements ActionListener 
{
private static final Dimension PREF_SIZE = new Dimension(1350, 450);

MyCanvas leftCanvas = new MyCanvas();
MyCanvas rightCanvas = new MyCanvas();
ArrayList<JButton> buttonList = new ArrayList<JButton>(); 
JPanel buttonPanel, leftPanel, rightPanel;

public GUI()
{
  super("Elevators");
    //setSize(800,800);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setVisible(true);

    JPanel mainPanel = new JPanel();

    mainPanel.setLayout(new GridLayout(1,3));

    leftPanel = new JPanel(); 
    leftPanel.add(leftCanvas);
    rightPanel = new JPanel();    
    rightPanel.add(rightCanvas);

    buttonPanel = new JPanel();     
    buttonPanel.setLayout(new GridLayout(12,1));   
    buttonPanel.setSize(450,450);




    add(mainPanel); 

    for(int i=0; i<12; i++)
    {
        buttonList.add(new JButton(""+i));
        JButton btn = buttonList.get(i);
        buttonPanel.add(btn);
        l
    }
    mainPanel.add(buttonPanel, BorderLayout.CENTER);
    mainPanel.add(leftPanel, BorderLayout.EAST);
    mainPanel.add(rightPanel, BorderLayout.WEST);

    createAndShowGui();
}
@Override
public Dimension getPreferredSize() {
    return PREF_SIZE;
}
private static void createAndShowGui() {
  UI frame = new UI();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.pack();
 // frame.setLocationByPlatform(true);
  frame.setVisible(true);
}


public void actionPerformed(ActionEvent e)
{

}

}    

私のCanvasクラス(まだあまりありません):

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;

public class MyCanvas extends Canvas 
{
private Elevator e;

int xPos =0;
int yPos=0;

public MyCanvas()
{
   setSize(600,600);
   repaint();
}


public void paint(Graphics g)
{
   g.setColor(Color.BLACK);
   g.fillRect(xPos,yPos,100, 100);


}

public void actionPerformed(ActionEvent e)
{
    repaint();
}
public void setElevator(Elevator ev)
{
    e = ev;
}
}

こんな顔になりたい こんな顔になりたい

今の様子
今の様子

4

1 に答える 1

1

あなたはレイアウトマネージャーと戦っています。結局、レイアウトマネージャーが勝ちます。

// You really shouldn't be doing this, mixing heavy and light weight components is
// simply asking for trouble.
//public class MyCanvas extends Canvas 
public class MyCanvas extends JPanel
{
    private Elevator e;

    int xPos =0;
    int yPos=0;

    public MyCanvas()
    {
       // Don't do this, the layout manager will overwrite the values
       //setSize(600,600);
       // Don't do this, it doesn't actually do anything as you ain't in
       // in position to paint yet...
       //repaint();
    }

    // Do this instead...
    public Dimension getPreferredSize() {
        return new Dimension(600, 600);
    }


    public void paint(Graphics g)
    {
        // You MUST do this...
        super.paint(g);
        g.setColor(Color.BLACK);
        g.fillRect(xPos,yPos,100, 100);
    }

    public void actionPerformed(ActionEvent e)
    {
        repaint();
    }

    public void setElevator(Elevator ev)
    {
        e = ev;
    }
}

時間をかけて読んでください:

今後に役立ついくつかの有用な情報について。

于 2012-10-30T23:07:02.940 に答える