在按钮中添加按钮

时间:2013-08-25 12:41:14

标签: java swing jtabbedpane imageicon

我在选项卡式窗格中有选项卡,它使用名为FilePanel的自定义类,这使得这有点令人困惑。它用于保存和加载文件,因此类文件面板。我想在每个标签的右侧添加一个图标按钮。单击时,它应该关闭该特定选项卡。

文件面板类:

package texteditor;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.io.File.*;
import java.awt.*;
import javax.swing.filechooser.FileNameExtensionFilter;

class FilePanel extends JPanel {

   private File file;
   private JTextArea textArea;
   private String name;


   public FilePanel(File file) throws FileNotFoundException, IOException {
      this.file = file;
      setLayout(new BorderLayout());

      textArea = new JTextArea();
      JScrollPane scroll = new JScrollPane(textArea);
      add(scroll, BorderLayout.CENTER);

      textArea.read(new FileReader(file.getAbsolutePath()), null);
      name = file.getName();


   }

   public FilePanel()  {

       try
       {
      setLayout(new BorderLayout());

      textArea = new JTextArea();
      JScrollPane scroll = new JScrollPane(textArea);
      add(scroll, BorderLayout.CENTER);

      textArea.read(new FileReader(file.getAbsolutePath()), null);
      name = file.getName();


       }
       catch(Exception e)
       {
           System.out.println(e);
       }

   }

   public File getFile() {
      return file;
   }

   public JTextArea getTextArea() {
      return textArea;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}

添加新框架方法:

private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {                                       
       increment++; 

       try
       {
          FilePanel p = new FilePanel();
          p.setName("File " + increment);

          //code im testing goes here and I just remove the 3 you see below.
          tabbedPane.add(p);
          tabbedPane.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
          tabbedPane.setSelectedComponent(p);

       }
       catch(Exception e)
       {
           System.out.println(e);
       }
    }  

我正在尝试但无济于事的代码:

Icon icon = new ImageIcon(getClass().getResource("redclose.png"));
         JButton butt = new JButton();
         butt.setIcon(icon);
         butt.setIconTextGap(5);
         butt.setHorizontalTextPosition(SwingConstants.RIGHT);
         tabbedPane.setTabComponentAt(0, butt);

这仅适用于图标本身:

Icon icon = new ImageIcon(getClass().getResource("redclose.png"));
          tabbedPane.setIconAt(tabbedPane.getSelectedIndex(), icon);

1 个答案:

答案 0 :(得分:1)

以下是可关闭标签的简单示例:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;

public class ClosableTab extends JPanel implements ActionListener{

    private final JTabbedPane pane;
    private final int index;

    private JButton closeButton;

    private JLabel titleLabel;

    public ClosableTab(final JTabbedPane pane, final int index){
        super(new BorderLayout());
        setOpaque(false);

        this.pane = pane;
        this.index = index;

        titleLabel = new JLabel(pane.getTitleAt(index));
        titleLabel.setHorizontalAlignment(JLabel.CENTER);

        closeButton = new JButton("X");
        closeButton.setBorder(new EmptyBorder(-5, 2, -5, 0));
        closeButton.setOpaque(false);
        closeButton.setContentAreaFilled(false);
        closeButton.setFont(closeButton.getFont().deriveFont(20F));
        closeButton.setForeground(Color.RED);
        closeButton.addActionListener(this);

        add(titleLabel, BorderLayout.CENTER);
        add(closeButton, BorderLayout.EAST);

        setPreferredSize(getPreferredSize());
    }

    public void actionPerformed(ActionEvent e){
        final Object source = e.getSource();
        if(source.equals(closeButton)){
            pane.remove(pane.indexOfTabComponent(this));
            pane.revalidate();
            pane.repaint();
        }
    }

    public void apply(){
        pane.setTabComponentAt(index, this);
        pane.revalidate();
        pane.repaint();
    }

    public static void apply(final JTabbedPane pane, final int index){
        new ClosableTab(pane, index).apply();
    }

}

添加FilePanel后,请尝试调用ClosableTab.apply(tabbedPane, tabbedPane.getTabCount()-1);