- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我写了一个小程序,在阅读一本关于 swing 的书时,它在两个标签之间创建了一个 JSplitPane。问题是 JSplitPane 几乎看不到(至少在我的操作系统 - MAC OS Lion 中)并且在其上设置一些属性(如前景色)似乎不起作用。
代码如下:
//Demonstrate a simple JSplitPane
package swingexample4_6;
import javax.swing.*;
import java.awt.*;
public class SplitPaneDemo {
//constructor
public SplitPaneDemo()
{
//Create a new JFrame container.
//Use the default border layout
JFrame jfrm = new JFrame("Split Pane Demo");
//Give the frame an initial size
jfrm.setSize(380, 150);
//Terminate the program when the user closes the application
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//--Make two labels to show the split pane
JLabel jlab = new JLabel(" Left side: ABCDEFGHIJKLMNOPQRSTUVWXYZ");
JLabel jlab2 = new JLabel(" Right side: ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//Set the minimum size for each label
//This step is not technically needed to use a split pane,
//but it enables the split pane resizing features to be
//used to their maximum extent
jlab.setMinimumSize(new Dimension(90, 30));
jlab2.setMinimumSize(new Dimension(90, 30));
//--Create a split pane
JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true, jlab, jlab2);
//Code to get a list of component names in the console
Component[] listComponents = jsp.getComponents();
String theList;
for (Component myComponent: listComponents)
{
theList = myComponent.toString();
System.out.println(theList);
}
//Add the split pane to the content pane
jfrm.getContentPane().add(jsp);
//Display the frame
jfrm.setVisible(true);
}
public static void main(String[] args) {
//Create the frame on the event dispatching thread
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new SplitPaneDemo();
}
});
}
}
有什么办法可以改变它的颜色,让它真正脱颖而出吗?谢谢。
最佳答案
您可以使用 SplitPane.background
属性,如下所示。
import javax.swing.*;
import java.awt.*;
/** @see http://stackoverflow.com/a/10110232/230513 */
public class SplitPaneDemo {
//constructor
public SplitPaneDemo() {
JFrame jf = new JFrame("Split Pane Demo");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//--Make two labels to show the split pane
JPanel left = content("Left side: ");
JPanel right = content("Right side: ");
//--Create a split pane
JSplitPane jsp = new JSplitPane(
JSplitPane.HORIZONTAL_SPLIT, true, left, right);
jsp.setDividerLocation(0.5f);
//Add the split pane to the frame's content pane
jf.add(jsp);
jf.pack();
//Display the frame
jf.setLocationRelativeTo(null);
jf.setVisible(true);
//Code to get a list of component names in the console
for (Component myComponent : jsp.getComponents()) {
System.out.println(myComponent);
}
}
private JPanel content(String s) {
final JLabel label = new JLabel(s + "Some text.", JLabel.CENTER);
JPanel panel = new JPanel(new GridLayout()) {
@Override
public Dimension getPreferredSize() {
Dimension d = label.getPreferredSize();
return new Dimension(d.width * 2, d.height * 3);
}
};
panel.setOpaque(true);
panel.setBackground(new Color(0xffffffc0));
panel.add(label);
return panel;
}
public static void main(String[] args) {
UIManager.put("SplitPane.background", new Color(0xff8080ff));
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SplitPaneDemo();
}
});
}
}
关于java - 如何更改 JSplitPane 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10109336/
下面是使用多个嵌套的 JSplitPanes 创建的简单布局的代码。 import java.awt.Color; import java.awt.Dimension; import java.awt
StackExchange 社区您好, 我对这个 JSplitPane 束手无策,我试图将其放入我的框架中,它位于我框架的右侧,而不是填充它或至少位于左侧。如果有人能帮助我解决这个问题,我将非常感激。
我有一个 Java GUI 程序,其中有一个 JSplitPane 来划分内容。 JSplitPane 的左侧是一个带有许多选项卡的 JTabbedPane - 一个非常复杂的布局。当左侧达到一定的复
只是想在屏幕上画一些线。 我已检查并确保所有相关代码都在运行 我已尝试调用 repaint(并确保其正在运行) 由于这是一个 JSplitPane,因此布局必须是 JSplitPane 布局 我正在设
我在通过鼠标调整 JSplitPane 的权重大小获取值时遇到一些问题。 这是我的代码: splitpane.setResizeWeight(0.1); splitpane.addPropertyCh
我在让 JSplitPane 按照我想要的方式工作时遇到了一些麻烦。 首先,我希望它完全填充其容器(JFrame),而不是由分割 Pane 内的组件确定其大小。其次,当我调整容器大小时,它不会在 JS
我遇到一个问题,我的 JSplitPane 有两个组件,左边是 JPanel,右边是第二个 JSplitPane包含JTextArea。 问题是,我的程序开始聚焦第二个 JSplitPane 的顶部(
我制作了一个 Java swing 应用程序。我的主类完成了整个 SwingUtilities.invokeLater(new Runnable() 的工作。 我的第二堂课,里面的一切都是我用过的:
我似乎无法通过调用 setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 来更改 JSplitPane 的调整大小光标; 有谁知道如何解决这个问题?我正在使用 N
我有一个包含两个部分的 JSplitPane。我想在单击按钮时隐藏正确的部分,并在再次单击同一个按钮时将其恢复。 这是我目前所拥有的: private void jButton1ActionPerfo
在我的应用程序中,我有一个显示 JSplitPane 的 JFrame,拆分为 VERTICAL_SPLIT。顶部显示一个JLabel,底部显示一个JInternalFrame。出现两个问题。 正在显
我有一个垂直拆分的 JSplitPane,当我向下移动分隔线时,它会移动底部组件并且底部被切断。有没有一种方法可以指定 JSplitPane 的调整大小行为,以便(底部组件的)顶部被拆分 Pane 覆
我在一个 JSplitPane 中组合了两个 JPanel。用户应该能够非常自由地移动 split-pane-divider,但分隔线不能太大(最好只有 1px)。 有没有办法将 dividerSiz
我不知道为什么,但 slider 只会向左移动,甚至锁定新位置。它使用 NestedJSplitPane 教程的修改版本以及 JTree 和 JEditorPane。我的猜测是,JEditorPane
我正在编写一个工具,用于在文本文件上执行任务。该任务需要一些时间才能执行,因此我制作了一个面板来显示文件名和进度百分比。用户可以在一个或多个文件上运行该任务,因此我需要为每个文件显示一个面板。 在帮助
我的 JSplitPane 有问题,我将组件设置为 JSplitPane 但没有显示任何内容。代码如下: /**__COMPONENT OBJECTS__**/ JFrame frame =
我想为我的 JSplitPane 制作自定义且更大的箭头,并在向上和向下箭头之间留出一些空间。 我当前的 JSplitPane 图像 最佳答案 I just have some problem wit
我有一个 JSPlitPane,它们之间有 50% 的分隔线。这样就可以正常工作了。 但是,当我在右侧添加一些 JLabels 时,jsplitpane 会忽略我的 50% 分隔符,左侧 Pane 会
我想要一个 jsplitPane 并在运行我的程序时用左组件交换右组件。我将划分位置设置为 0.2 左右。当我交换左分量和右分量并将分割位置设置为 0.8 时; jSplitPane 有问题。它被锁定
我找到了这个例子: http://java2s.com/Open-Source/Java-Document-2/Swing/petersoft-java-style-2/com/petersoft/w
我是一名优秀的程序员,十分优秀!