【问题标题】:JXDatePicker make arrows in the JXMonthView biggerJXDatePicker 使 JXMonthView 中的箭头更大
【发布时间】:2014-10-24 10:09:35
【问题描述】:

我想在将在触摸屏上工作的应用程序中使用这个 JXDatePicker 组件。因为默认组件很小,所以使用糟糕的触摸屏很难点击所有日期和按钮,我想让它们更大。到目前为止,我成功地使结果文本字段更大(显示所选日期的那个,通过更改字体),使弹出窗口更大(JXMonthView,也通过更改其字体),将 JXDatePicker 的图片更改为更大图片,将默认日期设置为当前日期,设置日期格式等。这是我的代码:

private void touch_screen_datepicker(JXDatePicker date_picker) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        JXMonthView monthView = date_picker.getMonthView();
        date_picker.setDate(new Date());
        date_picker.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
        JButton btn_pick = (JButton) date_picker.getComponent(1);
        btn_pick.setBackground(new Color(66, 147, 223));
        Image image = toolkit.getImage("/home/adrrian/Image/calendar/" + "calendar image 4.png"); //Земање на сликата за мк знаме
        ImageIcon icon = new ImageIcon(image); //Правење на икона
        btn_pick.setIcon(icon); //Поставување на иконата
        SimpleDateFormat longFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat shortFormat = new SimpleDateFormat("yy-MM-dd");
        Date startDate = new Date(0);//01.01.1970
        shortFormat.set2DigitYearStart(startDate);
        DatePickerFormatter formatter = new DatePickerFormatter(
                // invers sequence for parsing to satisfy the year parsing rules
                new DateFormat[]{shortFormat, longFormat}) {

                    @Override
                    public String valueToString(Object value) throws ParseException {
                        if (value == null) {
                            return null;
                        }
                        return getFormats()[1].format(value);
                    }
                };
        DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
        date_picker.getEditor().setFormatterFactory(factory);
        monthView.setFont(new Font(Font.DIALOG, Font.PLAIN, 50));
        monthView.setFirstDayOfWeek(Calendar.MONDAY);
    }

这是我最终作品的图片:

我的主要问题是如何制作改变月份的箭头(例如,如果我从这张图片返回显示九月)。我尝试列出所有组件,就像我为按钮所做的那样,但我仍然没有找到任何东西。同样为了更好的 GUI,我喜欢找到深蓝色(显示月份的位置),以使我的按钮相同。

希望有人可以帮助我。提前致谢。

    Toolkit toolkit = Toolkit.getDefaultToolkit();
            JXMonthView monthView = date_picker.getMonthView();
                /*EDITED*/
                //1
                date_picker.putClientProperty("JComponent.sizeVariant", "large");
                monthView.putClientProperty("JComponent.sizeVariant", "large");
                //2
                date_picker.putClientProperty("JXComponent.sizeVariant", "large");
                monthView.putClientProperty("JXComponent.sizeVariant", "large");
                //3
                date_picker.putClientProperty("JXDatePicker.sizeVariant", "large");
                monthView.putClientProperty("JXMonthView.sizeVariant", "large");
                //
                date_picker.putClientProperty("JDatePicker.sizeVariant", "large");
                monthView.putClientProperty("JMonthView.sizeVariant", "large");
                SwingUtilities.updateComponentTreeUI(this);
                SwingUtilities.updateComponentTreeUI(date_picker);
                SwingUtilities.updateComponentTreeUI(monthView);

                date_picker.updateUI();
                monthView.updateUI();
                /*EDITED*/ 

正如@Vighanesh Gursale 建议的那样,我插入了这些行,并且还在 setVisible(true) 之前执行了 frame.pack(),但没有任何变化。

【问题讨论】:

  • 您应该找到JXDatePickerLook and Feel 默认值。您可以通过更改默认值来更改按钮的大小,例如使用此链接。 docs.oracle.com/javase/tutorial/uiswing/lookandfeel/size.html
  • 对不起,它不能用它,我把它设置为大,但什么也没发生。
  • 你是如何尝试的,如果你能分享它会帮助你解决你的问题。
  • 我编辑了我的帖子并添加了一些代码行,我尝试了您的示例的许多组合,并且我做了 frame.pack(),然后设置为 true。我在框架的构造函数中调用了这个函数 touch_screen_datepicker。

标签: java user-interface datepicker


【解决方案1】:

我已经使用 nimbus 的外观和感觉制作了一些代码,我知道这并不完全符合您的要求,但它非常有帮助。检查此代码。要执行相同的操作,您需要找到下一个和上一个按钮的键,在我的情况下它是Button.margin。如果幸运的话,请尝试在代码中使用相同的密钥。

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

public class Demo {

    JFrame frame = new JFrame("");
    JButton btn = new JButton("Example");

    public Demo() {
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(btn);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            Insets insets = new Insets(50, 20, 50, 20); //change the size of button
            UIManager.put("Button.margin", insets);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        Demo d = new Demo();
    }
}

还要记住使用默认外观或任何其他外观。

【讨论】:

  • 我尝试使用常规按钮和日期选择器中的按钮。对于我的 JButton,它会更改边距,但在日期选择器上,不会做任何事情。感谢所有的帮助。
  • @AdrianES 会告诉你我是否有任何其他选项或代码。但是请检查此链接以获取 JDatePicker 另一个具有某些功能的 api,可能对您有所帮助。 sourceforge.net/projects/jdatepicker
【解决方案2】:

这个问题已经很老了,但对于所有正在研究如何完成的人来说

UIManager.put("JXMonthView.monthDownFileName", <PATH_TO_IMAGE>);
UIManager.put("JXMonthView.monthUpFileName", <PATH_TO_IMAGE>);

monthDownFileName 指上个月 monthUpFileName 指下个月

这将使用您选择的其他图像(和大小)更改图像

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2015-06-18
    相关资源
    最近更新 更多