问题描述:

I've developed an arduino project for weather measurements and i used visual basic for the gui.

Everything works fine with gauges , db connection and other things! Now i want to try my lack and write the same thing in java. I've written a small gui with serial communication and displaying the incoming data in jlabels. But now i want to use gauges here too.. they look so cool! I've found steelseries which is fantastic but i dont know from where to start. I've imported the library and junit in my project but now i'm blind and new in java and eclipse..for the gui i use window builder plugin. Is there anybody who knows how to starts using the library?

thank you!!

网友答案:

You can find many examples of SteelSeries gauges on on Harmonic Code. If you're new to Swing then start with Swing tutorials.

Here is a very basic example that demonstrates how to create a simple gauge and sets its value:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import eu.hansolo.steelseries.gauges.Radial;


public class TestGauge {
    private static void createAndShowUI() {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        JPanel panel = new JPanel() {
            @Override 
            public Dimension getPreferredSize() {
                return new Dimension(300, 300);
            }
        };

        final Radial gauge = new Radial();
        gauge.setTitle("Demo title");
        gauge.setUnitString("Some units");

        panel.setLayout(new BorderLayout());
        panel.add(gauge, BorderLayout.CENTER);
        frame.add(panel);

        JPanel buttonsPanel = new JPanel();
        JLabel valueLabel = new JLabel("Value:");

        final JTextField valueField = new JTextField(7);
        valueField.setText("30");
        JButton button = new JButton("Set");
        button.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double value = Double.valueOf(valueField.getText());
                    gauge.setValueAnimated(value);
                } catch(NumberFormatException ex) { 
                    //TODO - handle invalid input 
                    System.err.println("invalid input");
                }
            }
        });

        buttonsPanel.add(valueLabel);
        buttonsPanel.add(valueField);
        buttonsPanel.add(button);

        frame.add(buttonsPanel, BorderLayout.NORTH);

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}
相关阅读:
Top