我在使用名爲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);
你應該看看'setTabComponentAt(INT,組件)'; –