1

我在我的框架中添加了一个 tabbedpane 并调用 tab.add(" ",new Img()) 用 JPanel 扩展 Img ..

问题是:我可以在那个 JPanel 中添加 JScrollPane 和 drawImage 作为 JPanel 的背景,还可以在该图像上进行额外的绘图,例如在背景图像(例如地图)上制作路线,因为我想在这些路线上应用 Prim 的算法......

而且,如果我想像上面那样在 tabbedpane 上添加其他面板,我该如何控制这些选项卡操作..

示例代码是这样的......

如果您对 Prim 算法和图算法有任何想法,请帮助我!谢谢!

public class MainFrame extends JFrame {
    private JMenuBar menuBar = new JMenuBar();
    private JMenu menuFile = new JMenu();
    private JMenuItem menuFileExit = new JMenuItem();
    private JPanel jPanel1 = new JPanel();
    private JLabel lbl1=new JLabel();
    private JLabel lbl2=new JLabel();
    private JPanel jPanel2 = new JPanel();
    private JTabbedPane jTabbedPane1 = new JTabbedPane();
    private JPanel originalgraph = new JPanel();
    private JPanel zebuthiri = new JPanel();
    private JPanel dekhinathiri = new JPanel();
    private JPanel oattayathiri = new JPanel();
    private JPanel pobbathiri = new JPanel();
    private JPanel zeyathiri = new JPanel();
    int weight[][] = null;
    public MainFrame(int [][]w) {
        this.weight=w;
        try {
            jbInit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setJMenuBar( menuBar );
        this.getContentPane().setLayout(null);
        Toolkit tk=getToolkit();
        Dimension size=tk.getScreenSize();
        this.setSize( new Dimension(size) );
        this.getContentPane().setBackground(Color.CYAN);
        menuFile.setText( "File" );
        menuFileExit.setText( "Exit" );
        menuFileExit.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { fileExit_ActionPerformed( ae ); } } );
        jPanel1.setBounds(new Rectangle(0, 0, 1365, 160));
        jPanel1.setLayout(null);
        lbl1.setBounds(new Rectangle(0, 0, 1365, 160));
        lbl1.setIcon(new ImageIcon("5.jpg"));
        lbl2.setIcon(new ImageIcon("b.jpg"));
        jPanel2.setBounds(new Rectangle(0, 630, 1365, 160));
        lbl2.setBounds(new Rectangle(0, 0, 1365, 160));
        jPanel2.setLayout(null);
        jTabbedPane1.setBounds(new Rectangle(0, 160, 1365, 470));

        menuFile.add( menuFileExit );
        menuBar.add( menuFile );
        jPanel1.add(lbl1);
        jPanel2.add(lbl2);


        jTabbedPane1.addTab("Zebu Thiri",new zebuthiri(weight));
        jTabbedPane1.addTab("Original Graph",new originalgraph(weight));
        jTabbedPane1.addTab("Dekhina Thiri",new dekhinathiri(weight));
        jTabbedPane1.addTab("Oattaya Thiri",new oattayathiri(weight));
        jTabbedPane1.addTab("Pobba Thiri",new pobbathiri(weight));
        jTabbedPane1.addTab("Zeya Thiri",new zeyathiri(weight));
        this.getContentPane().add(jTabbedPane1, null);
        this.getContentPane().add(jPanel2, null);
        this.getContentPane().add(jPanel1, null);
    }

    void fileExit_ActionPerformed(ActionEvent e) {
        System.exit(0);
    }
    public static void main(String args[]){
        int w[][]=new int [100][100];
        MainFrame f=new MainFrame(w);
        f.setVisible(true);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
4

1 回答 1

2

请阅读如何将图像添加到 JPanel?

在小行中,图像可以像这样添加到 JPanel 上

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
       try {                
          image = ImageIO.read(new File("image name and path"));
       } catch (IOException ex) {
            // handle exception...
       }
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(image, 0, 0, null); 
                    // see javadoc for more info on the parameters
    }
}

在上面的示例中,图像在类的构造函数中加载,然后由paintComponent(...)执行构造函数后默认调用的

于 2012-07-13T05:51:43.013 回答