我试图迭代一个字符串,以删除重复字符.
例如,字符串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)
做这个的最好方式是什么?
我想要打印以下内容
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) 我想在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) 如何在一个按钮上添加多个样式...这不起作用.
<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"是否可以组合多种风格?
是否可以检查是否已选择/取消选择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) 为什么这不起作用?
我希望它每秒打印一次.
谢谢.
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) 我试图JButton在a的中心将两个s 放在彼此旁边,JFrame当JFrame重新调整大小时,不会重新调整按钮的大小.
为此,我将两个按钮放在带有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)