gpt4 book ai didi

Java Swing GUI,自定义JtabbedPane风格

转载 作者:行者123 更新时间:2023-12-02 14:45:31 25 4
gpt4 key购买 nike

我是 Swing 初学者。我尝试创建一个选项卡式窗口作为我的元素 GUI 的一部分。但如下所示,用于导航选项卡的按钮显示某种边框。有什么办法可以去除这些边框吗?请参阅附图以查看问题。

enter image description here

GUI代码如下

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class tst {
/**
* @param args the command line arguments
*/
public static String generateHtml(String tabButtonLabel,String style) {
/*@@Generates HTML for the tab button when the button label is given*/
String ret = "<html><body style = '"+style+"'>"+tabButtonLabel+"</body></html>";
return ret;
}

public static void main(String[] args) {
// TODO code application logic here
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JFrame frame = new JFrame("tst");
frame.setVisible(true);
frame.setSize(screenSize);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*Groups tab*/
JPanel groups = new JPanel();
groups.setBackground(Color.gray);
/*Settings Tab*/
JPanel settings = new JPanel();
/*About Tab*/
JPanel about = new JPanel();
/*Tabbed pane to hold all panels*/
JTabbedPane tabbedPane = new JTabbedPane();
/*Tabbed Pane CSS*/
String tabButtonCss = "margin:0;width:110px;height:10px;border-radius:3px;padding:10px;background:#fff;text-align:center;border:none;";

tabbedPane.setVisible(true);
tabbedPane.add(generateHtml("Groups",tabButtonCss),groups);
tabbedPane.add("Settings",settings);
tabbedPane.add("About",about);
/*Tab styles*/
tabbedPane.setBackground(Color.gray);
tabbedPane.setForeground(Color.white);
tabbedPane.setBounds(0, 0, 0,0);
//tabbedPane.setBorder(new EmptyBorder(-10,-20,-10,0));
frame.add(tabbedPane);
}
}

最佳答案

正如 @Smit 所指出的,这一切都取决于 CSS。另请注意,Java HTML/CSS 引擎无法识别颜色的“3 位数字”样式快捷方式 #fff。要变白,我们必须使用#ffffff

Css1 Css2 Css3

import java.awt.*;
import javax.swing.*;

class ExampleCSS {

public static void showStyle(String style) {
JPanel gui = new JPanel(new BorderLayout());

String html1 = "<html><body style = '"+style+"'>";
String html2 = "</body></html>";
JTabbedPane tp = new JTabbedPane();
tp.addTab(html1 + "Groups" + html2, new JLabel(style));
tp.addTab(html1 + "Settings" + html2, new JLabel(style));
tp.addTab(html1 + "About" + html2, new JLabel(style));

gui.add(tp);

JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setContentPane(gui);
f.setMinimumSize(new Dimension(400,100));
f.pack();
f.setLocationByPlatform(true);
f.setVisible(true);
}

public static void main(String[] args) {
Runnable r = new Runnable() {

@Override
public void run() {
String[] css = {
"margin:0;background:#ffffff;",
"padding:10px;",
"width:110px;height:10px;border-radius:3px;"
+ "text-align:center;border:none;"
};
showStyle(css[0]);
showStyle(css[0]+css[1]);
showStyle(css[0]+css[1]+css[2]);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}

另请参阅 @whiskeyspider 针对 BG color 的 @trashgod 解决方案的建议。比尝试在 HTML 中强制执行要好得多。

关于Java Swing GUI,自定义JtabbedPane风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15622446/

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