Java 通过按下另一个面板中的按钮,将按钮动态添加到一个面板

Java 通过按下另一个面板中的按钮,将按钮动态添加到一个面板,java,swing,graphics,Java,Swing,Graphics,我在一个框架上创建了一个主面板 在该面板上还有两个面板。我想要的是,我在panel1上有按钮,现在,单击按钮后,我想在第二个面板上绘制一个2d对象 我做了很多尝试,但都没有成功。请在这种情况下帮助我。任何帮助都将不胜感激。 谢谢 看看 您可以向按钮添加操作并将面板作为参数传递此代码中有太多疯狂的东西。我想这是你想要做的: import java.awt.event.ActionEvent; import javax.swing.*; import java.awt.*; import java.

我在一个框架上创建了一个主面板

在该面板上还有两个面板。我想要的是,我在panel1上有按钮,现在,单击按钮后,我想在第二个面板上绘制一个2d对象

我做了很多尝试,但都没有成功。请在这种情况下帮助我。任何帮助都将不胜感激。 谢谢

看看


您可以向按钮添加操作并将面板作为参数传递

此代码中有太多疯狂的东西。我想这是你想要做的:

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.border.Border;

public class Iframe extends JFrame implements ActionListener {
    public JPanel jp0 = new JPanel( new GridLayout() );
    public JPanel jp1 = new JPanel( new GridLayout( 1, 3 ) );
    public JPanel jp2 = new JPanel( new BorderLayout() );
    Myclass       my  = new Myclass( 100, 200, 300, 400, 500, 600, 700, 900, 800, 10000, 3000, 600 );

    public static void main( String args[] ) {
        new Iframe();
    }

    public Iframe() {
        Border border = BorderFactory.createTitledBorder( "Pentahexes" );
        jp1.setBorder( border );

        ImageIcon Pentaheximg1 = new ImageIcon( "E:/NetBeans/Pentahex-Game/src/pentahex/game/Pentahex1.jpg", "b1" );
        JButton Pentahexbutton1 = new JButton( Pentaheximg1 );
        Pentahexbutton1.addActionListener( this );
        Pentahexbutton1.setName( "b1" );
        jp1.add( Pentahexbutton1 );

        ImageIcon Pentaheximg2 = new ImageIcon( "E:/NetBeans/Pentahex-Game/src/pentahex/game/Pentahex2.jpg", "b1" );
        JButton Pentahexbutton2 = new JButton( Pentaheximg2 );
        Pentahexbutton2.addActionListener( this );
        Pentahexbutton2.setName( "b2" );
        jp1.add( Pentahexbutton2 );

        ImageIcon Pentaheximg3 = new ImageIcon( "E:/NetBeans/Pentahex-Game/src/pentahex/game/Pentahex3.jpg", "b1" );
        JButton Pentahexbutton3 = new JButton( Pentaheximg3 );
        Pentahexbutton3.addActionListener( this );
        Pentahexbutton3.setName( "b3" );
        jp1.add( Pentahexbutton3 );

        Border border2 = BorderFactory.createTitledBorder( "Puzzle" );
        jp2.setBorder( border2 );

        jp0.add( jp1 );
        jp0.add( jp2 );
        jp2.add( my );
        this.add( jp0 );
        this.add( jp2 );
        this.add( jp1, BorderLayout.EAST );
        this.setExtendedState( JFrame.MAXIMIZED_BOTH );
        this.setVisible( true );
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
        if( "b1".equals( ((JButton)e.getSource()).getName() ) ) {
            my.setData( 100, 200, 300, 400, 500, 600, 700, 900, 800, 10000, 3000, 600 );
        }
        if( "b2".equals( ((JButton)e.getSource()).getName() ) ) {
            my.setData( 10, 20, 30, 400, 50, 60, 70, 80, 90, 100, 110, 120 );
        }
        if( "b3".equals( ((JButton)e.getSource()).getName() ) ) {
            my.setData( 10, 30, 45, 400, 60, 75, 90, 80, 90, 1000, 3010, 120 );
        }
        my.drawpoly();
    }
}

class Myclass extends JPanel {
    int a, b, c, d, e, f, g1, h, i, j, k, l;

    Myclass( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x5, int y5, int x6, int y6 ) {
        setData( x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6 );
    }

    public void setData( int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, int x5, int y5, int x6, int y6 ) {
        a = x1;
        b = y1;

        c = x2;
        d = y2;

        e = x3;
        f = y3;

        g1 = x4;
        h = y4;

        i = x5;
        j = y5;

        k = x6;
        l = y6;
    }

    public void drawpoly() {
        invalidate();
        repaint();
    }

    @Override
    public void paintComponent( Graphics g ) {
        super.paintComponent( g );
        int xcoordinatesofthehexagon[] = new int[] { a, c, e, g1, i, k };
        int ycoordinatesofthehexagon[] = new int[] { b, d, f, h, j, l };
        g.drawPolygon( xcoordinatesofthehexagon, ycoordinatesofthehexagon, 6 );
    }
}
更新

import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.border.Border;

public class Iframe extends JFrame implements ActionListener {

    private static final String PATH = "E:/NetBeans/Pentahex-Game/src/pentahex/game/";

    HashMap<String, int[]> pentahexMap = new HashMap<String, int[]>() {{

        put( "Clear",     new int[]{ 0,0,0,0,0,0,0,0,0,0,0,0 } );
        put( "Pentahex1", new int[]{ 0,0,0,50,50,50,25,25,50,0,0,0 } );
        put( "Pentahex2", new int[]{ 100,100,100,150,150,150,125,125,150,100,100,100 } );
        put( "Pentahex3", new int[]{ 10, 30, 45, 400, 60, 75, 90, 80, 90, 500, 310, 120 } );

    }};

    private Myclass my = new Myclass();

    public static void main( String args[] ) {
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run() {
                new Iframe().setVisible( true );
            }
        });
    }

    public Iframe() {

        JPanel jp0 = new JPanel( new GridLayout() );
        JPanel jp1 = new JPanel( new GridLayout( 1, 3 ) );
        JPanel jp2 = new JPanel( new BorderLayout() );

        for( Entry<String, int[]> entry : pentahexMap.entrySet() ) {
            JButton pentahexbutton1;
            try {
                URL imgUrl                = new URL(PATH + entry.getKey() +".jpg");
                ImageIcon pentaheximg1    = new ImageIcon( imgUrl );
                pentahexbutton1           = new JButton( pentaheximg1 );
            } catch( MalformedURLException e ) {
                pentahexbutton1 = new JButton( entry.getKey() );
            }
            pentahexbutton1.addActionListener( this );
            pentahexbutton1.setName( entry.getKey() );
            jp1.add( pentahexbutton1 );
        }

        jp1.setBorder( BorderFactory.createTitledBorder( "Pentahexes" ) );
        jp2.setBorder( BorderFactory.createTitledBorder( "Puzzle" ) );

        jp0.add( jp1 );
        jp0.add( jp2 );
        jp2.add( my );
        this.add( jp0 );
        this.add( jp2 );
        this.add( jp1, BorderLayout.EAST );
        this.setExtendedState( JFrame.MAXIMIZED_BOTH );
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
        int[] data = pentahexMap.get( ((JButton)e.getSource()).getName() );
        if(data != null) {
            my.setData( data );
            my.invalidate();
            my.repaint();
        }
    }
}

class Myclass extends JPanel {
    int a, b, c, d, e, f, g, h, i, j, k, l;
    Myclass(){}
    Myclass( int... data ) {
        setData( data );
    }
    public void setData( int... data ) {
        if(data.length < 12) {
            throw new IllegalArgumentException("not enough data");
        }
        a = data[0];
        b = data[1];
        c = data[2];
        d = data[3];
        e = data[4];
        f = data[5];
        g = data[6];
        h = data[7];
        i = data[8];
        j = data[9];
        k = data[10];
        l = data[11];
    }
    @Override
    public void paintComponent( Graphics graphics ) {
        super.paintComponent( graphics );
        int[] xcoordinatesofthehexagon = new int[] { a, c, e, g, i, k };
        int[] ycoordinatesofthehexagon = new int[] { b, d, f, h, j, l };
        if(!isEmpty(xcoordinatesofthehexagon, ycoordinatesofthehexagon)){
            graphics.drawPolygon( xcoordinatesofthehexagon, ycoordinatesofthehexagon, 6 );
        }
    }
    private boolean isEmpty(int[] x,int[] y) {
        for( int i : y ) {
            if(i != 0) {
                return false;
            }
        }
        for( int i : x ) {
            if(i != 0) {
                return false;
            }
        }
        return true;
    }
}
导入java.awt.event.ActionEvent;
导入javax.swing.*;
导入java.awt.*;
导入java.awt.event.ActionListener;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.HashMap;
导入java.util.Map.Entry;
导入javax.swing.border.border;
公共类Iframe扩展JFrame实现ActionListener{
私有静态最终字符串PATH=“E:/NetBeans/Pentahex-Game/src/Pentahex/Game/”;
HashMap pentahexMap=新HashMap(){{
put(“清除”,新int[]{0,0,0,0,0,0,0,0,0,0});
put(“五进制1”,新int[]{0,0,0,50,50,25,50,0,0});
put(“五角体2”,新int[]{100100150125125150100100});
put(“五角体3”,新的int[]{10,30,45,400,60,75,90,80,90,500,310,120});
}};
private Myclass my=new Myclass();
公共静态void main(字符串参数[]){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新建Iframe().setVisible(true);
}
});
}
公共Iframe(){
JPanel jp0=newjpanel(newgridlayout());
JPanel jp1=新JPanel(新网格布局(1,3));
JPanel jp2=新的JPanel(newborderlayout());
for(条目:pentahexpap.entrySet()){
JButton pentahexbutton1;
试一试{
URL imgUrl=新URL(PATH+entry.getKey()+“.jpg”);
ImageIcon pentaheximg1=新的ImageIcon(imgUrl);
pentahexbutton1=新的JButton(pentaheximg1);
}捕获(格式错误){
pentahexbutton1=新的JButton(entry.getKey());
}
pentahexbutton1.addActionListener(此);
pentahexbutton1.setName(entry.getKey());
jp1.添加(五角按钮1);
}
jp1.setboorder(BorderFactory.createTitledBorder(“五角形”);
jp2.setboorder(BorderFactory.createTitledBorder(“拼图”);
jp0.add(jp1);
jp0.add(jp2);
jp2.加入(my);;
本条增补(jp0);
本条增补(jp2);
add(jp1,BorderLayout.EAST);
setExtendedState(JFrame.MAXIMIZED_两者);
}
@凌驾
已执行的公共无效操作(操作事件e){
int[]data=pentahexMap.get(((JButton)e.getSource()).getName());
如果(数据!=null){
my.setData(数据);
我的.invalidate();
我的;
}
}
}
类Myclass扩展了JPanel{
int a,b,c,d,e,f,g,h,i,j,k,l;
Myclass(){}
Myclass(整数…数据){
setData(数据);
}
公共无效设置数据(整数…数据){
如果(数据长度<12){
抛出新的IllegalArgumentException(“数据不足”);
}
a=数据[0];
b=数据[1];
c=数据[2];
d=数据[3];
e=数据[4];
f=数据[5];
g=数据[6];
h=数据[7];
i=数据[8];
j=数据[9];
k=数据[10];
l=数据[11];
}
@凌驾
公共虚空绘制组件(图形){
super.paintComponent(图形);
int[]xcoordinatesofthehehehehexagon=新的int[]{a,c,e,g,i,k};
int[]ycoordinatesofthehehehehexagon=新的int[]{b,d,f,h,j,l};
如果(!isEmpty(xcoordinatesofthehexagon,ycoordinatesofthehexagon)){
绘图多边形(X六边形坐标,Y六边形坐标,6);
}
}
私有布尔值为空(int[]x,int[]y){
for(int i:y){
如果(i!=0){
返回false;
}
}
for(int i:x){
如果(i!=0){
返回false;
}
}
返回true;
}
}

它可以是按钮/2d图像。对不起,fr d spam。你能保留元素和吗?@aishwarya:谢谢,但我试过了,现在我只是想看看上面的事情是否发生了。下次会有3-4个面板,可见性会很混乱。嘿@oliholz:非常感谢你的回答。只有一个问题。当我加载框架时,我看到的是面板上已经绘制的2d图像。但是,我想要的是在我点击按钮1之后,只有它应该被加载到面板上。其余的工作正常。谢谢。有了更新的代码,你可以在
pentahexMap
中放置新数据,它可以在不改变布局的情况下工作-(键与按钮名称相同),并且在启动时它是空的,你可以清除。:)
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.border.Border;

public class Iframe extends JFrame implements ActionListener {

    private static final String PATH = "E:/NetBeans/Pentahex-Game/src/pentahex/game/";

    HashMap<String, int[]> pentahexMap = new HashMap<String, int[]>() {{

        put( "Clear",     new int[]{ 0,0,0,0,0,0,0,0,0,0,0,0 } );
        put( "Pentahex1", new int[]{ 0,0,0,50,50,50,25,25,50,0,0,0 } );
        put( "Pentahex2", new int[]{ 100,100,100,150,150,150,125,125,150,100,100,100 } );
        put( "Pentahex3", new int[]{ 10, 30, 45, 400, 60, 75, 90, 80, 90, 500, 310, 120 } );

    }};

    private Myclass my = new Myclass();

    public static void main( String args[] ) {
        SwingUtilities.invokeLater( new Runnable() {
            @Override
            public void run() {
                new Iframe().setVisible( true );
            }
        });
    }

    public Iframe() {

        JPanel jp0 = new JPanel( new GridLayout() );
        JPanel jp1 = new JPanel( new GridLayout( 1, 3 ) );
        JPanel jp2 = new JPanel( new BorderLayout() );

        for( Entry<String, int[]> entry : pentahexMap.entrySet() ) {
            JButton pentahexbutton1;
            try {
                URL imgUrl                = new URL(PATH + entry.getKey() +".jpg");
                ImageIcon pentaheximg1    = new ImageIcon( imgUrl );
                pentahexbutton1           = new JButton( pentaheximg1 );
            } catch( MalformedURLException e ) {
                pentahexbutton1 = new JButton( entry.getKey() );
            }
            pentahexbutton1.addActionListener( this );
            pentahexbutton1.setName( entry.getKey() );
            jp1.add( pentahexbutton1 );
        }

        jp1.setBorder( BorderFactory.createTitledBorder( "Pentahexes" ) );
        jp2.setBorder( BorderFactory.createTitledBorder( "Puzzle" ) );

        jp0.add( jp1 );
        jp0.add( jp2 );
        jp2.add( my );
        this.add( jp0 );
        this.add( jp2 );
        this.add( jp1, BorderLayout.EAST );
        this.setExtendedState( JFrame.MAXIMIZED_BOTH );
    }

    @Override
    public void actionPerformed( ActionEvent e ) {
        int[] data = pentahexMap.get( ((JButton)e.getSource()).getName() );
        if(data != null) {
            my.setData( data );
            my.invalidate();
            my.repaint();
        }
    }
}

class Myclass extends JPanel {
    int a, b, c, d, e, f, g, h, i, j, k, l;
    Myclass(){}
    Myclass( int... data ) {
        setData( data );
    }
    public void setData( int... data ) {
        if(data.length < 12) {
            throw new IllegalArgumentException("not enough data");
        }
        a = data[0];
        b = data[1];
        c = data[2];
        d = data[3];
        e = data[4];
        f = data[5];
        g = data[6];
        h = data[7];
        i = data[8];
        j = data[9];
        k = data[10];
        l = data[11];
    }
    @Override
    public void paintComponent( Graphics graphics ) {
        super.paintComponent( graphics );
        int[] xcoordinatesofthehexagon = new int[] { a, c, e, g, i, k };
        int[] ycoordinatesofthehexagon = new int[] { b, d, f, h, j, l };
        if(!isEmpty(xcoordinatesofthehexagon, ycoordinatesofthehexagon)){
            graphics.drawPolygon( xcoordinatesofthehexagon, ycoordinatesofthehexagon, 6 );
        }
    }
    private boolean isEmpty(int[] x,int[] y) {
        for( int i : y ) {
            if(i != 0) {
                return false;
            }
        }
        for( int i : x ) {
            if(i != 0) {
                return false;
            }
        }
        return true;
    }
}