小编Ric*_*cco的帖子

从Java中的String中删除重复项

我试图迭代一个字符串,以删除重复字符.

例如,字符串aabbccdef应该成为abcdef ,字符串abcdabcd应该成为abcd

这是我到目前为止:

public class test {

    public static void main(String[] args) {

        String input = new String("abbc");
        String output = new String();

        for (int i = 0; i < input.length(); i++) {
            for (int j = 0; j < output.length(); j++) {
                if (input.charAt(i) != output.charAt(j)) {
                    output = output + input.charAt(i);
                }
            }
        }

        System.out.println(output);

    }

}
Run Code Online (Sandbox Code Playgroud)

做这个的最好方式是什么?

java string

16
推荐指数
4
解决办法
17万
查看次数

java写入文本文件

我想要打印以下内容

TEST1

TEST2

TEST3

TEST4

但我似乎无法将文本提交到下一行.

请帮忙

import java.io.*;

public class MainFrame {
    public static void main(String[] args) {
        try {
        BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));
            for (int i = 0; i < 4; i++) {
                out.write("test " + "\n");
            }
            out.close();
        } catch (IOException e) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

java

9
推荐指数
2
解决办法
9万
查看次数

更改JTextArea中不同行的字体

我想在JTextArea中添加不同的字体行,但最后一种字体似乎会覆盖另一种字体.

请帮忙...

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

@SuppressWarnings("serial")
public class test extends JFrame {

private static JTextArea referenceTextArea = new JTextArea(10, 10);
private JPanel panel = new JPanel();

public test() {
    this.add(panel);
    panel.add(referenceTextArea);
}

public static void textTest() {
    referenceTextArea.setFont(new Font("Serif", Font.BOLD, 15));
    referenceTextArea.append("line1");
    referenceTextArea.append("\n");

    referenceTextArea.setFont(new Font("Serif", Font.ITALIC, 30));
    referenceTextArea.append("line2");
    referenceTextArea.append("\n");
}

public static void main(String[] args) {
    test frame = new test();
    frame.setVisible(true);
    frame.setSize(400, 400);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

    textTest();
}
}
Run Code Online (Sandbox Code Playgroud)

java user-interface swing

7
推荐指数
1
解决办法
2万
查看次数

HTML按钮样式

如何在一个按钮上添加多个样式...这不起作用.

<button style="background:red", "cursor:pointer">click me</button>
Run Code Online (Sandbox Code Playgroud)

我试过了:

  • style="background:red", "cursor:pointer"
  • style="background:red" "cursor:pointer"
  • style="background:red" style="cursor:pointer"

是否可以组合多种风格?

html button

4
推荐指数
2
解决办法
8万
查看次数

检查是否选择了JTextfield

是否可以检查是否已选择/取消选择jtextfield(即已单击文本字段并且光标现在位于字段内)?

//编辑感谢下面的帮助,这是一个有效的例子

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class test extends JFrame {

private static JPanel panel = new JPanel();
private static JTextField textField = new JTextField(20);
private static JTextField textField2 = new JTextField(20);

public test() {
    panel.add(textField);
    panel.add(textField2);
    this.add(panel);
}

public static void main(String args[]) {

    test frame = new test();
    frame.setVisible(true);
    frame.setSize(500, 300);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    textField.addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            System.out.println("selected");
        }

        @Override
        public void focusLost(FocusEvent e) { …
Run Code Online (Sandbox Code Playgroud)

java swing focus jtextfield

3
推荐指数
2
解决办法
2万
查看次数

java计时器将无法正常工作

为什么这不起作用?

我希望它每秒打印一次.

谢谢.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

public class test2 {

public static void main(String[] args) {

    Timer timer = new Timer(1000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("hello");
        }
    });

    timer.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

java timer

2
推荐指数
1
解决办法
4036
查看次数

BorderLayout中心命令不会居中

我试图JButton在a的中心将两个s 放在彼此旁边,JFrameJFrame重新调整大小时,不会重新调整按钮的大小.

为此,我将两个按钮放在带有a的面板中,FlowLayout然后放在带有中心的面板中BorderLayout.

但是,以下代码不会在中心显示所选面板BorderLayout.

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class test extends JFrame {

    private JPanel panel1 = new JPanel();
    private JPanel panel2 = new JPanel();
    private JButton button1 = new JButton("one");
    private JButton button2 = new JButton("two");

    public test() {
        panel1.setLayout(new FlowLayout());
        panel1.add(button1);
        panel1.add(button2);

        panel2.setLayout(new BorderLayout());
        panel2.add(panel1, BorderLayout.CENTER);

        this.add(panel2);
    }

    public static void main(String[] args) {
        test frame = new test(); …
Run Code Online (Sandbox Code Playgroud)

java layout swing

2
推荐指数
1
解决办法
1317
查看次数

标签 统计

java ×6

swing ×3

button ×1

focus ×1

html ×1

jtextfield ×1

layout ×1

string ×1

timer ×1

user-interface ×1