0

私はswingで絵を描く初心者です。チュートリアルを読みましたが、理解できません。JDialogs でユーザーから入力を取得し、それらの値を使用して線を描画しようとすると、次にそれらを整数値に解析して何も起こりません。助けてください。誰かが私にopenglバインディングJOGLを使うように言ったのですか?これはコードです:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DrawingPanel extends JPanel{
int x0,y0,x1,y1;
DrawingPanel(int ix,int iy,int fx,int fy){
    setLayout(new GridBagLayout());
    x0=ix;
    y0=iy;
    x1=fx;
    y1=fy;
}
public void paintComponent(Graphics g){

    g.drawLine(x0,y0,x1,y1);
}
}
class DDA implements ActionListener{
JFrame j;
JDialog jd;
JButton ok,can;
JTextField ix,iy,fx,fy;
int x0,y0,xf,yf;
DDA(){
    j=new JFrame("DDA Algorithm");
    j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    j.setLocationByPlatform(true);
    j.setLayout(new GridBagLayout());
    j.setExtendedState(Frame.MAXIMIZED_BOTH);
    //j.setSize(500, 500);
    createandrun();
    DrawingPanel ob=new DrawingPanel(x0,y0,xf,yf);

    GridBagConstraints c=new GridBagConstraints();
    c.anchor=GridBagConstraints.PAGE_START;
    j.add(ob,c);
    //j.add(jd);
    j.setResizable(false);
    j.setVisible(true);
    //j.pack();
}
public void createandrun(){
JLabel lx,ly,rx,ry;

jd=new JDialog((Frame)null,"Input Box",true);
//jd.setSize(250, 250);
jd.setLocationRelativeTo(j);
jd.setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();

lx=new JLabel("X0 : ");
ly=new JLabel("Y0 : ");
rx=new JLabel("X1 : ");
ry=new JLabel("Y1 : ");
ix=new JTextField(10);
iy=new JTextField(10);
fx=new JTextField(10);
fy=new JTextField(10);
ok=new JButton("OK");
can=new JButton("Cancel");

c.anchor=GridBagConstraints.PAGE_START;
c.insets=new Insets(5,5,0,5);
c.gridx=c.gridy=0;
jd.add(lx,c);
c.gridx=1;
jd.add(ix,c);
c.gridx=0;
c.gridy=1;
jd.add(ly,c);
c.gridx=1;
jd.add(iy,c);
c.gridx=0;
c.gridy=2;
jd.add(rx,c);
c.gridx=1;
jd.add(fx,c);
c.gridx=0;
c.gridy=3;
jd.add(ry,c);
c.gridx=1;
jd.add(fy,c);
c.gridx=0;
c.gridy=4;
c.insets=new Insets(10,5,5,5);
jd.add(ok,c);
c.gridx=1;
//c.fill=GridBagConstraints.HORIZONTAL;
jd.add(can,c);
ok.addActionListener(this);
can.addActionListener(this);

jd.pack();
jd.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
    if(e.getSource()==can){
        jd.dispose();
    }
    if(e.getSource()==ok){
        try{
            x0=Integer.parseInt(ix.getText());
            y0=Integer.parseInt(iy.getText());
            xf=Integer.parseInt(fx.getText());
            yf=Integer.parseInt(fy.getText());
            }
            catch(NumberFormatException ex){
                JOptionPane.showMessageDialog(ok,"Please only numbers!","Invalid Input", JOptionPane.INFORMATION_MESSAGE);
            }
        jd.dispose();
    }
}

public static void main(String s[]){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new DDA();
        }
    });
}
}
4

2 に答える 2

1

またはプロパティが設定されていないため、何も表示されないため、必要に応じて展開しないGridBagConstraintsでください。あなたがすることができます:JFramefillweightDrawingPanel

c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;

または、単純に のデフォルトBorderLayoutを使用して、次JFrameのようにパネルを追加することもできます。

j.add(ob);

補足:子コンポーネントのペイントを呼び出すsuper.paintComponent(g)ときは、忘れずに呼び出してください。paintComponent

于 2013-03-17T14:43:06.613 に答える
0

それが機能するためには、いくつかの特定のアクションを実行する必要があります。

の代わりにメソッドを定義paintしますclass DDApaintComponent

public void paint(Graphics g){
    g.drawLine(x0, y0, x1, y1);
}

DrawingPanelオブジェクトのインスタンス化を in クラスに移動obactionPerformed methodDDAレイアウト定義を完了します。間違いの 1 つは、行パラメーターとして使用している変数であるload の前であってもDrawingPanel、コンストラクター メソッドでコンストラクターを呼び出していることです。そのため、 メソッドの最後に次のようなものを含めます。DDAx0, y0, xf, yfactionPerformed

DrawingPanel ob = new DrawingPanel(x0, y0, xf, yf);
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.PAGE_START;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1;
c.weighty = 1;
j.add(ob, c);
j.setResizable(false);
j.setVisible(true);

次のように、before ブロックの後にオブジェクトpaintのメソッドを呼び出します。DrawingPanelactionPerformedDDA

ob.repaint(); 
于 2013-03-17T15:23:30.860 に答える