gpt4 book ai didi

java - 将按钮添加到选项卡中

转载 作者:行者123 更新时间:2023-12-01 14:12:13 24 4
gpt4 key购买 nike

我在选项卡式 Pane 中使用了名为 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);

最佳答案

下面是可关闭选项卡的一个简单示例:

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);

关于java - 将按钮添加到选项卡中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18429082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com